Asp.net 使用复选框从gridview获取值
我有这个gridview,我试图打印出用户选中的任何列的MMBR\u PROM\u ID (Default.apsx)Asp.net 使用复选框从gridview获取值,asp.net,gridview,Asp.net,Gridview,我有这个gridview,我试图打印出用户选中的任何列的MMBR\u PROM\u ID (Default.apsx) 欢迎使用ASP.NET! (Default.aspx.cs) 受保护的无效页面加载(对象发送方,事件参数e) { 如果(!IsPostBack) { FrontOffenties tmpdb=新的FrontOffenties(); List newlistmbr_Prom=tmpdb.MMBR_Prom.ToList(); GridView1.DataSource=newLi
欢迎使用ASP.NET!
(Default.aspx.cs)
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
FrontOffenties tmpdb=新的FrontOffenties();
List newlistmbr_Prom=tmpdb.MMBR_Prom.ToList();
GridView1.DataSource=newListMMBR_Prom;
GridView1.DataBind();
}
}
因此,我的目标是,当我按下generate时,我希望能够以字符串形式打印出用户检查的所有MMBR_PROM_ID。我对aspnet有点陌生,所以我很难按照您提到的要求处理语法。您可以尝试下面给定的代码,在单击“生成”按钮时从Gridview1获取MMBR\u PROM\u ID的值
//For every row in the grid
foreach (GridViewRow r in GridView1.Rows)
{
//Find the checkbox in the current row being pointed named as grdViewCheck
CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");
//Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
if (chk!=null && chk.Checked)
{
Response.Write(r.Cells[1].Text);
}
}
这里,单元格[1]是指特定行的单元格索引,在您的情况下,它是要打印的MMBR\u PROM\u ID。希望这有帮助
如果要查找逗号分隔的值MMBR\u PROM\u ID,下面提到的代码适用于您
//Declaration of string variable
string str="";
//For every row in the grid
foreach (GridViewRow r in GridView1.Rows)
{
//Find the checkbox in the current row being pointed named as grdViewCheck
CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");
//Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
if (chk!=null && chk.Checked)
{
Response.Write(r.Cells[1].Text);
//appending the text in the string variable with a comma
str = str + r.Cells[1].Text + ", ";
}
}
//Printing the comma seperated value for cells[1] which is MMBR_PROM_ID
Response.Write(str);
谢谢,成功了。我怎么能用逗号把它分开呢?并将其置于“生成”按钮下?您是否希望在逗号分隔的字符串中看到所有选中的行MMBR\u PROM\u ID?如果是,那么您需要在foreach上面声明一个字符串变量,并在If条件中使用stringVar+=r.cells[1].Text+“,”如何声明字符串变量?我已经发布了另一个代码,我认为这将满足您的要求。
//For every row in the grid
foreach (GridViewRow r in GridView1.Rows)
{
//Find the checkbox in the current row being pointed named as grdViewCheck
CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");
//Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
if (chk!=null && chk.Checked)
{
Response.Write(r.Cells[1].Text);
}
}
//Declaration of string variable
string str="";
//For every row in the grid
foreach (GridViewRow r in GridView1.Rows)
{
//Find the checkbox in the current row being pointed named as grdViewCheck
CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");
//Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
if (chk!=null && chk.Checked)
{
Response.Write(r.Cells[1].Text);
//appending the text in the string variable with a comma
str = str + r.Cells[1].Text + ", ";
}
}
//Printing the comma seperated value for cells[1] which is MMBR_PROM_ID
Response.Write(str);