隐藏列为第一列,checkbox添加到模板列中,利用它的OnCheckedChanged事件执行代码,填充行号到footer
页面代码如下
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="rowNumber" runat="server">12</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="lblmsg" runat="server">你点击了第</asp:Label>
</FooterTemplate>
<ControlStyle Width="200px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="rowNumber" runat="server">12</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="lblmsg" runat="server">你点击了第</asp:Label>
</FooterTemplate>
<ControlStyle Width="200px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码如下
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
//Response.Write();
GridViewRow row = ((CheckBox)sender).Parent.Parent as GridViewRow;
TextBox tb = (TextBox)(row.FindControl("rowNumber"));
GridView1.FooterRow.Cells[1].Text = tb.Text;
//Response.Write(tb.Text);
}
{
//Response.Write();
GridViewRow row = ((CheckBox)sender).Parent.Parent as GridViewRow;
TextBox tb = (TextBox)(row.FindControl("rowNumber"));
GridView1.FooterRow.Cells[1].Text = tb.Text;
//Response.Write(tb.Text);
}
测试通过,谢谢1!