首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

gridview+checkbox 老是得不到选中的行

2013-12-11 
gridview+checkbox 总是得不到选中的行我用gridview加checkbox,想修改一下选中的某行,总是得不到checkbox.

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+"");
                }
            }
        }

里面的  if (cb.Checked == true)总是执行不了?这是怎么回事?


[解决办法]
前台html片段


<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton2" runat="server"  CommandName="edit" CommandArgument="<%#Eval("你的主键字段") %>">编辑</asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>


后台处理编辑,处理GridView的RowCommand事件

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "edit")    //你在前台编辑按钮指定的CommandName
    {
        //获取你的数据记录的主键
        int id = Convert.ToInt32(e.CommandArgument);
        //你的其它操作
        //...
    }
}

[解决办法]
(CheckBox)GridView1.Rows[i].FindControl("CheckBox");
改成
(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox");
[解决办法]
for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");//你的CheckBox的ID号好似不对                if (cb.Checked == true)
                {
                    int buildid =Convert.ToInt32(GridView1.Rows[i].Cells[2].ToString());
                    Response.Redirect("../EstateResidentManagement/BuildAddUpdate.aspx?buildid="+buildid+"");
                }
            }

热点排行