C#GridView动态CheckBox消失了

时间:2017-04-21 12:21:55

标签: c# asp.net

我有一个相当简单的应用程序,它返回一个失败的报告列表,其中包含ID,Name和Time Bound字段以及左侧的Checkbox模板字段。

我有一个'重新安排'按钮,当按下该按钮时,应该选中勾选复选框的行并处理它们。

GridView成功加载,我可以选择/取消选中CheckBoxes但是当我按下'Reschedule'按钮并返回代码时,复选框不再存在。

我知道这与动态控制/回发有关,并且需要重新创建复选框,我已经尝试过对以前类似问题的大量建议,但没有任何效果

GridView - AutoGenerate Columns False(尝试为true)

按钮 - OnClientClick =“”(尝试返回false)

这些字段最初是创建并绑定到数据表(数据表有3列映射到3个绑定字段),如下所示: -

TemplateField tfield = new TemplateField();
failedSchedulesGridView.Columns.Add(tfield);

BoundField bfield1 = new BoundField();
bfield1.HeaderText = "SI_ID";
bfield1.DataField = "si_id";
failedSchedulesGridView.Columns.Add(bfield1);

BoundField bfield2 = new BoundField();
bfield2.HeaderText = "SI_NAME";
bfield2.DataField = "si_name";
failedSchedulesGridView.Columns.Add(bfield2);

BoundField bfield3 = new BoundField();
bfield3.HeaderText = "SI_UPDATE_TS";
bfield3.DataField = "si_update_ts";
failedSchedulesGridView.Columns.Add(bfield3);

failedSchedulesGridView.DataSource = dt;
failedSchedulesGridView.DataBind();

的Page_Load 可以看出我已经尝试在这里重新创建GridView列但它没有工作并被注释掉

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //TemplateField tfield = new TemplateField();
        //failedSchedulesGridView.Columns.Add(tfield);

        //BoundField bfield1 = new BoundField();
        //bfield1.HeaderText = "SI_ID";
        //bfield1.DataField = "si_id";
        //failedSchedulesGridView.Columns.Add(bfield1);

        //BoundField bfield2 = new BoundField();
        //bfield2.HeaderText = "SI_NAME";
        //bfield2.DataField = "si_name";
        //failedSchedulesGridView.Columns.Add(bfield2);

        //BoundField bfield3 = new BoundField();
        //bfield3.HeaderText = "SI_UPDATE_TS";
        //bfield3.DataField = "si_update_ts";
        //failedSchedulesGridView.Columns.Add(bfield3);

    }
   failedSchedulesGridView.DataSource = dt;
   failedSchedulesGridView.DataBind();
}

OnRowDataBound

protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.Header)
    {
        cbx++;
        CheckBox cb = new CheckBox();
        cb.ID = "cb" + cbx;
        e.Row.Cells[0].Controls.Add(cb);
    }
}

由于未找到checbox,因此在按下“重新计划”按钮后尝试访问复选框时,代码失败: -

protected void ReschedulePB2_Click(object sender, EventArgs e)
{
    int i = 0;
    foreach (GridViewRow row in failedSchedulesGridView.Rows)
    {
        i++;
        string cbName = "cb" + i;
        CheckBox cb = (CheckBox)row.Cells[0].FindControl(cbName);

        if (cb.Checked)

1 个答案:

答案 0 :(得分:0)

试试这个

将此内容写入您的aspx页面

<asp:GridView ID="failedSchedulesGridView" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"  Width="850px" onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField >
                <HeaderTemplate>
                    <asp:CheckBox ID="cbHeader" runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="cbItem" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="si_id" HeaderText="SI_ID" SortExpression="si_id" />
            <asp:BoundField DataField="si_name" HeaderText="SI_NAME" SortExpression="si_name" />
            <asp:BoundField DataField="si_update_ts" HeaderText="SI_UPDATE_TS" SortExpression="si_update_ts" />
        </Columns>
</asp:GridView>

您的网页加载应该如下所示

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        failedSchedulesGridView.DataSource = dt;
        failedSchedulesGridView.DataBind();
    }
}

您的重新安排点击将是

protected void ReschedulePB2_Click(object sender, EventArgs e)
{

    foreach (GridViewRow row in failedSchedulesGridView.Rows)
    {
        CheckBox cb = (CheckBox)row.Cells[0].FindControl("cbItem");

        if (cb.Checked)