locked
Checkbox remains selected after page postback - Issue RRS feed

  • Question

  • User2015884762 posted

    Hi, let me explain the error scenario

    I select the checkbox, there are 5 of them and the repeater control gets populated accordingly. Here, there are no issues during the selection. 

    Then, I go and unselect the checbox for the first row, the page refreshes and the checkbox remains selected.

    I try again , and at the second time, the checkbox would get deselected fine.

    Why is this happening. This is my code, am I missing something. 

    public void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                //List<string> user_select_weeks_list = new List<string>();
                AllLists create1 = new AllLists();
    
    
    
                for (int i = 0; i < rptItemsInCart.Items.Count; i++)
                {
                    CheckBox ref_to_checkBox = (CheckBox)rptItemsInCart.Items[i].FindControl("Checkbox1");
                    if (ref_to_checkBox.Checked)
                    {
                        Ok_prompt_to_upload_Button.Visible = true;
                        ref_to_checkBox.ID = "Week" + (i + 1);
                        Label2.Text = ref_to_checkBox.ID;
                        create1.user_select_weeks_list.Add(ref_to_checkBox.ID);
                        //user_select_weeks_list.Add(ref_to_checkBox.ID);
                    }
                    else
                    {
                        create1.user_select_weeks_list.Remove(ref_to_checkBox.ID);
                    }
                }
    
                
                selected_weeks_repeater.DataSource = create1.user_select_weeks_list;
                selected_weeks_repeater.DataBind();
                
                
    
            }
     public class AllLists
       {
            public List<string> user_select_weeks_list { get; set; }
    
            public AllLists()
            {
                this.user_select_weeks_list = new List<string>();
            }
        }

    I did some debugging, but of course, I was not able to debug as the checkbox did not get fired, but i did see this exception in Visual Studio

    'this.IsValid' threw an exception of type 'System.Web.HttpException' 

    "Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate."

    Wednesday, January 23, 2019 7:31 AM

All replies

  • User-1174608757 posted

    Hi callykalpana,

     ref_to_checkBox.ID = "Week" + (i + 1);

    The situation happens because you change the ID of the checkbox. When CheckedChanged( ) function run  , program will send  the id of checkbox which  is selected. However you have changed the ID when first the select the checkbox. When next time you select , the program couldn;t get  the correct id. so it will not run the function.So, I suggest you to create a new string , then it will work well.

    Here is the demo .I hope it could help you.

    aspx:

    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="Repeater1" runat="server">                  
                    <ItemTemplate>
                  <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"  OnCheckedChanged="CheckBox1_CheckedChanged"/> <%#Eval("fruit") %><br>
                    </ItemTemplate>
                </asp:Repeater>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </div>
        </form>
    </body>
    </html>

    aspx.cs:

        public partial class Button : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string sql = "select * from db";
                    this.Repeater1.DataSource = SqlHelper.ExecuteDataTable(sql);
                    this.Repeater1.DataBind();
    
                }
            }
    
    
            public class AllLists
            {
                public List<string> user_select_weeks_list { get; set; }
    
                public AllLists()
                {
                    this.user_select_weeks_list = new List<string>();
                }
            }
    
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                AllLists create1 = new AllLists();
    
                for (int i = 0; i < Repeater1.Items.Count; i++)
                {
                    CheckBox ref_to_checkBox = (CheckBox)Repeater1.Items[i].FindControl("Checkbox1");
                    if (ref_to_checkBox.Checked)
                    {                  
                      
                        string abc = "Week" + (i + 1);
                        create1.user_select_weeks_list.Add(abc);
                        
                    }
                    else
                    {
                        create1.user_select_weeks_list.Remove(ref_to_checkBox.ID);
                    }
                }
    
    
               GridView1.DataSource = create1.user_select_weeks_list;
               GridView1.DataBind();
    
    
    
            }
        }

    Best Regards

    Wei Zhang

    Thursday, January 24, 2019 3:31 AM