服务器控件后台获取checkbox选取的值,获取不到值
protected void Button1_Click(object sender, EventArgs e)
{
this.txtResult.Text = string.Empty;
if (grid.Rows.Count > 0)
{
foreach (GridViewRow row in grid.Rows)
{
HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
if (_chkItem != null && _chkItem.Checked)
{
this.txtResult.Text += _chkItem.Value + ",";
}
}
if (this.txtResult.Text.Length > 0)
{
this.txtResult.Text = this.txtResult.Text.Substring(0, this.txtResult.Text.Length - 1); }
}请教大虾代码哪里有问题
protected void Button1_Click(object sender, EventArgs e)
{
this.txtResult.Text = string.Empty;
if (grid.Rows.Count > 0)
{
foreach (GridViewRow row in grid.Rows)
{
HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
if (_chkItem != null && _chkItem.Checked)
{
this.txtResult.Text += _chkItem.Value + ",";
}
}
if (this.txtResult.Text.Length > 0)
{
this.txtResult.Text = this.txtResult.Text.Substring(0, this.txtResult.Text.Length - 1); }
}请教大虾代码哪里有问题
{
CheckBox chk= (CheckBox)gvr.FindControl("CheckBox1");
if (chk!=null&& chk.Checked)
{
GridViewRow row = chk.NamingContainer as GridViewRow;
string s=dr.Cells[0].Text;}
改成
CheckBox _chkItem = (CheckBox)row.FindControl("chkItem");
试试
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" OnPreRender="grid_PreRender">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type="checkbox" id="chkAll" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chkItem" value='<%# Eval("LOT_NB") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LOT_NB" HeaderText="LOT_NB" />
<asp:BoundField DataField="ORD_DT" HeaderText="ORD_DT" />
<asp:BoundField DataField="COMP_DT" HeaderText="COMP_DT" />
<asp:BoundField DataField="BASELOC_CD" HeaderText="BASELOC_CD" />
</Columns>
</asp:GridView>页面代码
HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
应该会报“未将对象引用到”错误,但你的却没有报错,这说明id和类型转换是对的。单击按钮时先执行Page_Load方法,是不是把GridView里的东西又给初始化了?