【发布时间】:2017-06-11 01:38:34
【问题描述】:
我进行了很多搜索,其中包括 checkchanged 或 OncheckChanged 关键字:
ASP.NET CheckBox does not fire CheckedChanged event when unchecking
https://forums.asp.net/t/1311576.aspx?Checkbox+not+firing+when+unchecking+using+OnCheckedChanged
OnCheckedChanged event not firing
OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked
但这似乎不起作用,尽管我应用了上面给出的链接中的所有建议
我想从嵌套的 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>
我正在尝试使用CBoxAvailabilityTime1CheckBox。正如你们所看到的,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 skips 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();
}
}
【问题讨论】:
-
如何将数据绑定到 gvDocSchedule?能出示一下代码吗?
-
我应该编辑我的问题还是摆弄它?
-
请发布最小可行代码。或者,向您显示 this 之类的内容,以便我们重现问题。
标签: c# asp.net gridview webforms