目标:
我想在Checkbox的OnCheckedChanged事件触发时更新数据库中复选框的状态。此复选框位于gridview的每一行。不想回发整个页面,所以我在Updatepanel中有gridview。
问题:
将gridview放入更新面板后,我无法触发OnCheckedChanged事件。
或者我是以错误的方式接近这个?
以下是我对updatepanel,gridview,复选框和复选框事件代码的了解
此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();
}
答案 0 :(得分:0)
这种做法似乎没有错!但是,是
UpdateMode="Conditional"
有问题吗?
答案 1 :(得分:0)
您必须尝试在cbREVISION_CheckChanged
事件中绑定嵌套网格视图:
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
}
它会起作用!