0

I have conducted a lot of searches which included checkchanged or OncheckChanged keyword:

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

But this just doesn't seem to be working although I applied all suggestion from the links given above

I am tring to fire a CheckChanged Event From nested gridview, whose DataSource gets binded in parent grid's OnRowDataBound event.

My aspx markup

<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>

I was trying it with CBoxAvailabilityTime1CheckBox. As you guys can see ViewStateMode and EnableViewState has been taken care of not only in server tag but also in content page's tag, also these properties are true for master page

Parent Grid gets binded when user press a button and nested grid gets binded in parent's OnRowDataBound event

Here is My aspx.cs code

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

With this setup CheckChanged Event Fires on Checking. On Checking it hits Page_ Load skips if(!IsPostBack) (as AutoPostBack=true) and then control is directly transferred to CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e) event handler function but on the other hand it doesn't fire on unchecking it postsbacks goes to Page load again Skips if(!IsPostBack) and does nothing instead of calling CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)

"Note:-" Page_Load is not involved. I can't bind parent grid in page_load, !IsPostBack because I dont need to bind it at first time when page gets loaded rather its binding happens inside a button click event, if a button is clicked only then parent grid must get binded.

Update 1:- This is how I bind data to parent grid

I am calling this function in a button click event

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();
         }
    }
Community
  • 1
  • 1
Ammar Bayg
  • 105
  • 10

1 Answers1

0

Tested a stripped down version of you snippet. It seems to be working. When you check CBoxAvailabilityTime1, it disables CBoxAvailabilityTime2. When you uncheck CBoxAvailabilityTime1 the other one is enabled again.

However this is only when the DataBinding of gvDocSchedule is inside an IsPostBack check. When it's not the checkboxes will always go to their default unchecked state after 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();
}

Or in your case something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindDataToGridViewDocInArea();
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • I can't bind my gridview in page load I have mentioned it already, in my question, my gridview binding has to take place after a button click. What should i do in this case should I call button click event in page_load like btn_click(sender, e); this will also misbehave because there are few selections to be made from dropdownlists and I am providing ids from dropdowns to sql query which is in button click event – Ammar Bayg Jan 25 '17 at 19:58
  • Even if I move `BindDataToGridViewDocInArea();` to a Button click event it still works as it should. – VDWWD Jan 25 '17 at 20:05
  • I tried to move `BindDataToGridViewDocInArea();` to `Page_Load` inside `(!IsPostBack)` still CheckChanged event firing on checking and not hitting on unchecking. When you moved `BindDataToGridViewDocInArea();` in button click u take it off from `!IsPostback` right? – Ammar Bayg Jan 25 '17 at 20:13
  • Also I don't understand with `AutoPostBack` on set for check boxes it is posting back everytime it is hitted, why would it hit some thing inside `!IsPostBack`. And my check box is unselected by default when it appears, it gets selected right, and disables the other but when I again try to click it to unselect it goes to page load **Skips** `IsPostBack` and does nothing instead of calling checkedchanged event and checkbox remains checked as i did it in first place – Ammar Bayg Jan 25 '17 at 20:23
  • Yes I did. The only thing I did use was my own function to bind data to the Grid. Now I'm taking a good look at `BindDataToGridViewDocInArea`. It seems you use a value from `ddlAskArea`. What happens if you give `@AID` a fixed value for testing? – VDWWD Jan 25 '17 at 20:23
  • Ok let me check – Ammar Bayg Jan 25 '17 at 20:25
  • And it seems you don't understand the asp.net page life cycle. Read [this](http://stackoverflow.com/documentation/asp.net/4948/page-life-cycle#t=201701252024259503626) and [this](http://stackoverflow.com/questions/4251157/what-is-a-postback) – VDWWD Jan 25 '17 at 20:26
  • I did it and for once somehow or other it seems that it hitted CheckChanged event on unchecking and instead of enabling Cbox2 again the child grid displayed empty. I clean and rebuilt but it never hitted checkchanged event again on unchecking with @AID static – Ammar Bayg Jan 25 '17 at 20:43