【问题标题】:Update a gridview row within a Updatepanel by the click of a checkbox not working通过单击不工作的复选框来更新更新面板中的网格视图行
【发布时间】:2017-10-04 07:02:29
【问题描述】:

目标:

我想在 Checkbox 的 OnCheckedChanged 事件触发时更新数据库中复选框的状态。此复选框位于 gridview 的每一行。不想回发整个页面,所以我在 Updatepanel 中有 gridview。

问题:

将网格视图放置在更新面板中后,我无法触发 OnCheckedChanged 事件。

还是我用错了方法?


这是更新面板、网格视图、复选框和复选框事件代码

这个gridview的绑定在if (!IsPostBack)内

即使在下面的示例中未显示,该 Gridview 也嵌套在另一个 gridview 中,如果这会有所不同的话。

HTML

<asp:UpdatePanel ID="gridUpdatePanel" UpdateMode="Conditional" runat="server" ChildrenAsTriggers="false">  
<ContentTemplate>
     <asp:GridView ID="gvComponents" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid" OnRowDataBound="gvComponents_RowDataBound" ShowHeader="false">
        <Columns> 
            //Other TemplateFields
            <asp:TemplateField HeaderText="Revisions Required" Visible="false"  ItemStyle-Width="10%" >
                <ItemTemplate>
                    <div style="text-align:center;">
                        <asp:CheckBox ID="cbREVISION_REQD"  runat="server" Enabled="true" Checked='<%# (bool)Eval("REVISION") %>' AutoPostBack="true" OnCheckedChanged ="cbREVISION_CheckChanged" />                                               
                    </div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</ContentTemplate> 

C#

protected void cbREVISION_CheckChanged(object sender, EventArgs e)
{
    //code to update the database
   gridUpdatePanel.Update();
}

【问题讨论】:

  • 所以.....显然这是有效的。我只是调试错了。

标签: c# asp.net gridview updatepanel


【解决方案1】:

您必须尝试在cbREVISION_CheckChanged 事件中绑定nested-gridview:

protected void cbREVISION_CheckChanged(object sender, EventArgs e)
    {
        GridViewRow gvr = ((CheckBox)sender).Parent as GridViewRow; // gets the gridview row where checkbox cliked

        GridView gv = gvr.Parent as GridView; // gets the cliked gridview or nested gridview

        CheckBox chkbox = gvr.FindControl("cbREVISION_REQD") as CheckBox; // gets checkbox from cliked gridview

        bool status = chkbox.Checked; // gets the status of the checkbox

        // here bind your nested-gridview
        gv.DataSource = dt; // dt is some data to which you set gridview
        gv.DataBind(); // binding methed to bind gridview

    }

它会起作用的!

【讨论】:

    【解决方案2】:

    这种方法似乎没有任何问题!但是,是

    UpdateMode="Conditional"
    

    有问题吗?

    【讨论】:

    • 有条件的 UpdateMode 背后的想法是不更新每次回发,无论它来自页面上的哪个位置。你觉得这个逻辑有问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    相关资源
    最近更新 更多