2017-05-05 104 views
0

目标复选框的点击更新的UpdatePanel内的GridView的一行:通过不工作

我想,当OnCheckedChanged复选框火灾的事件来更新数据库中的复选框的状态。该复选框位于gridview的每一行上。不想回发整个页面,所以我有一个UpdatePanel内的gridview。

问题:

我不能让OnCheckedChanged触发事件,一旦GridView控件放置在UpdatePanel中。

还是我接近这个错误的方式?


这里是我有

绑定该GridView控件的是内部的if (!IsPostBack)

即使它不是在下面这个例子所示的UpdatePanel,GridView控件,复选框,复选框事件代码,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(); 
} 
+0

所以.....显然这是工作。我只是在调试错误。 –

回答

0

似乎有什么不妥的做法!但是,是

UpdateMode="Conditional" 

有问题吗?

+0

作为有条件的UpdateMode背后的想法是不会在每次回发时更新,无论它来自何处。 你看到这个逻辑有问题吗? –

0

你一定要试试这在cbREVISION_CheckChanged事件绑定嵌套的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 

    } 

很有效!