[英]OnCheckChanged Event of asp:CheckBox Not Firing When Unchecked From Within Nested GridView
我进行了很多搜索,其中包括checkchanged或OncheckChanged关键字:
取消选中时,ASP.NET CheckBox不会触发CheckedChanged事件
https://forums.asp.net/t/1311576.aspx?复选框+不+触发+何时+取消选中+使用+ OnCheckedChanged
取消选中复选框时,不会触发asp:checkbox的OnCheckedChanged事件处理程序
但这似乎不起作用,尽管我应用了上面给出的链接中的所有建议
我正试图从嵌套的gridview中触发一个CheckChanged事件 ,该事件的DataSource
绑定在父网格的OnRowDataBound
事件中。
我的aspx标记
<asp:GridView ID="gvDocSchedule" runat="server" AutoGenerateColumns="false" CssClass="table table-striped color-black"
OnRowDataBound="gvDocSchedule_RowDataBound" DataKeyNames="UserID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor:pointer" src="UserPanel/images/Plus12.png" />
<asp:Panel ID="PanelSchedule" runat="server" Style="display:none">
<asp:GridView ID="gvScheduleDayNTime" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField ="ScheduleDay" HeaderText="CheckInTime1"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckInTime1" HeaderText="CheckInTime1"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckOutTime1" HeaderText="CheckOutTime1"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckInTime2" HeaderText="CheckInTime2"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckOutTime2" HeaderText="CheckOutTime2"/>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnForChkBox" runat="server" value="No" />
<asp:CheckBox ID="CBoxAvailabilityTime1" ViewStateMode="Enabled" Checked="false" Enabled="true" EnableViewState="true" runat="server" Text="See Free TimeSlots For Booking In Time1" AutoPostBack="true" OnCheckedChanged="CBoxAvailabilityTime1_CheckedChanged" />
<br />
<asp:CheckBox ID="CBoxAvailabilityTime2" runat="server" Text="See Free TimeSlots For Booking In Time1" AutoPostBack="true" OnCheckedChanged="CBoxAvailabilityTime2_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ControlStyle-Font-Bold="true" DataField="UserFirstName" HeaderText="Doctor's First Name" />
<asp:BoundField DataField="UserLastName" HeaderText="Doctor's Last Name" />
<asp:BoundField DataField="SpecializationName" HeaderText="Doctor's Specialization" />
<asp:BoundField DataField="HospitalName" HeaderText="Hospital" />
</Columns>
</asp:GridView>
我正在尝试使用CBoxAvailabilityTime1
CheckBox。 你们可以看到ViewStateMode和EnableViewState不仅在服务器标签中而且在内容页的标签中都得到了照顾,这些属性对于母版页也适用
当用户按下按钮时,父网格被绑定,而嵌套网格在父对象的OnRowDataBound
事件中被绑定
这是我的aspx.cs代码
protected void CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvParentRow in gvDocSchedule.Rows)
{
if (gvParentRow.RowType == DataControlRowType.DataRow)
{
GridView gvDocSchedulesGetChildGrid = (GridView)gvParentRow.FindControl("gvScheduleDayNTime");
if (gvDocSchedulesGetChildGrid != null)
{
foreach (GridViewRow gvChildRow in gvDocSchedulesGetChildGrid.Rows)
{
CheckBox CBoxAvailabilityTime1 = (CheckBox)gvChildRow.FindControl("CBoxAvailabilityTime1");
CheckBox CBoxAvailabilityTime2 = (CheckBox)gvChildRow.FindControl("CBoxAvailabilityTime2");
if (((CheckBox)CBoxAvailabilityTime1).Checked)
{
CBoxAvailabilityTime2.Enabled = false;
}
if (!((CheckBox)CBoxAvailabilityTime1).Checked)
{
CBoxAvailabilityTime2.Enabled = true;
}
}
}
}
}
}
使用此设置,CheckChanged事件在检查时触发。 在检查它击中Page_ Load
跳过 if(!IsPostBack)
为AutoPostBack=true
),然后控制被直接传送到CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)
事件处理函数 ,但是,另一方面,它不会触发取消选中它postsbacks
进入Page load
再次跳过 if(!IsPostBack)
和做的,而不是调用什么CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)
“注意:-”不涉及Page_Load
。 我无法在page_load, !IsPostBack
绑定父网格,因为在页面加载时我不需要在第一次时绑定它,而是在按钮单击事件内发生绑定,如果仅单击按钮,则必须绑定父网格。
更新1:- 这就是我将数据绑定到父网格的方式
我在按钮单击事件中调用此函数
protected void BindDataToGridViewDocInArea()
{
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmdFillGridDocInArea = new SqlCommand("some query with parameter here", con)
cmdFillGridDocInArea.Parameters.AddWithValue("@AID", ddlAskArea.SelectedValue);
SqlDataAdapter sdaFillGridDocInArea = new SqlDataAdapter(cmdFillGridDocInArea);
DataTable dtFillGridDocInArea = new DataTable();
sdaFillGridDocInArea.Fill(dtFillGridDocInArea);
if (dtFillGridDocInArea.Rows.Count > 0)
{
gvDocSchedule.DataSource = dtFillGridDocInArea;
gvDocSchedule.DataBind();
}
else
{
lblError.Text = string.Empty;
lblError.Text = "No Record Exists Against Requst Specified";
}
con.Dispose();
}
}
测试了您的摘要的简化版本。 它似乎正在工作。 当您检查CBoxAvailabilityTime1
,它将禁用CBoxAvailabilityTime2
。 取消选中CBoxAvailabilityTime1
,另一个将再次启用。
但是,仅当gvDocSchedule
的数据绑定位于IsPostBack
检查中时。 否则,复选框在回发后将始终进入其默认的未选中状态。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//this works
gvDocSchedule.DataSource = LoadFromDB();
gvDocSchedule.DataBind();
}
//this does not
gvDocSchedule.DataSource = LoadFromDB();
gvDocSchedule.DataBind();
}
或者在您的情况下,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDataToGridViewDocInArea();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.