asp的OnCheckChanged事件:从嵌套GridView中取消选中时,CheckBox不会触发

时间:2017-01-25 18:30:10

标签: c# asp.net gridview webforms

我进行了很多搜索,其中包括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

但是,虽然我从上面给出的链接中应用了所有建议,但这似乎并没有起作用

我想从嵌套网格视图中触发 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)

&#34;注意: - &#34; 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();
         }
    }

1 个答案:

答案 0 :(得分:0)

测试了您的片段的精简版本。它似乎工作。当您检查CBoxAvailabilityTime1时,它会停用CBoxAvailabilityTime2。取消选中CBoxAvailabilityTime1时,会再次启用另一个。

但是,仅当gvDocSchedule的数据绑定在IsPostBack项检查中时才会这样。如果不是,则在PostBack之后复选框将始终进入默认的未选中状态。

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();
    }
}