gridview+checkbox 总是得不到选中的行
我用gridview加checkbox,想修改一下选中的某行,总是得不到checkbox.checked的这一行。
前台代码:
<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="buildname" HeaderText="楼栋名称" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="buildid" HeaderText="楼栋编号" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="sumfloors" HeaderText="总楼层数" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="sumsets" HeaderText="总户数" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="use" HeaderText="楼栋用处">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="buildarea" HeaderText="建筑面积" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
protected void imgbtnUpdate_Click(object sender, ImageClickEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (cb.Checked == true)
{
int buildid =Convert.ToInt32(GridView1.Rows[i].Cells[2].ToString());
Response.Redirect("../EstateResidentManagement/BuildAddUpdate.aspx?buildid="+buildid+"");
}
}
}
[解决办法]
前台html片段
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit" CommandArgument="<%#Eval("你的主键字段") %>">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit") //你在前台编辑按钮指定的CommandName
{
//获取你的数据记录的主键
int id = Convert.ToInt32(e.CommandArgument);
//你的其它操作
//...
}
}