[英]how to get row values when checkbox is checked in gridview
before I asked this I did some checking around to make sure that this doesn't turn out to be a duplicate and ways to get the row values from a row that has a checkbox in its template field...but I can't seem to get it working...So far I have tried 在我问这个问题之前,我做了一些检查以确保这不会成为重复项,以及如何从在其模板字段中具有复选框的行中获取行值的方法...但是我似乎无法使它起作用...到目前为止我已经尝试过
protected void Page_Load(object sender, EventArgs e)
{
Entities NW = new Entities();
var customers =
from c in NW.Customers
where (c.ContactName.StartsWith("Ma") || c.ContactName.StartsWith("An")
|| c.ContactName.StartsWith("T")
|| c.ContactName.StartsWith("V")
)
orderby c.ContactName ascending
select new
{
c.CustomerID,
c.ContactName,
c.CompanyName,
c.City
};
gv1.DataSource = customers.ToList();
gv1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("CB") as CheckBox);
if (chkRow.Checked)
{
Label1.Text = row.Cells[2].Text;
}
}
}
}
I have stepped through the button click event and when it gets to 我已经逐步完成了按钮单击事件以及何时到达
if (chkRow.Checked)
如果(chkRow.Checked)
its showing as null and skips over it.. 其显示为null并跳过它。
my markup is 我的标记是
<asp:GridView ID="gv1" runat="server">
<HeaderStyle BackColor="SkyBlue" />
<AlternatingRowStyle BackColor="Yellow" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CB" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
when I look at the source after I run, the checkboxes are all named differently than what I gave them the ID of "CB", attached is the pic of the source when its running 运行后查看源代码时,复选框的名称与我给它们指定的ID“ CB”的名称不同,所附是源代码在运行时的图片
I am not sure what I am doing wrong with this 我不确定我在做什么错
Your GridView
should have more columns, because you are referencing Cell[2]
in your code. 您的
GridView
应该有更多的列,因为您在代码中引用Cell[2]
。
You might try to use row
object to look for your control: 您可能尝试使用
row
对象来查找控件:
CheckBox chkRow = (row.FindControl("CB") as CheckBox);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.