Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've a asp:CheckBox and a asp:Label on my ASP.NET page:
ASP
<asp:CheckBox ID="checkBox1" runat="server" OnCheckedChanged="checkBox1_CheckedChanged" Text="I agree" /><br />
<asp:Label ID="label1" runat="server" Text="Check the check box if you agree." />

I added a CheckedChanged event handler to the asp:CheckBox:
C#
protected void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (checkBox1.Checked)
        {
            label1.Text = "You agree. Please click on the button to go to the next page.";
        }
        else
        {
            label1.Text = "Check the check box if you agree.";
        }
    }
}

But if I check/uncheck the check box, the label text isn't changing. Why is the label text not changing, and how to fix it?

Thanks in advance.
Posted
Updated 22-Dec-12 0:02am
v2
Comments
Avinash_Pathak 22-Dec-12 6:07am    
I think you should remove if (!IsPostBack)...because whatever you put inside this...will execute on first time only when page is loaded...and set autopostback true to checkbox
Thomas Daniels 22-Dec-12 6:10am    
Hi,

Thanks for your reply!
If I add AutoPostBack = true, and if I remove (!IsPostBack), then it's working.

1 solution

Add 'AutoPostback = true' for the checkbox - once done, it should trigger the OnCheckedChanged event and thus the label change.
 
Share this answer
 
v2
Comments
Thomas Daniels 22-Dec-12 6:08am    
Hi,

Thanks for your reply.
I tried this, but the label text is still not changing.
[EDIT]
But if I add this, and if I remove the if (!IsPostBack) (see the comment from Avinash_Pathak), then it's working.
Sandeep Mewara 22-Dec-12 6:12am    
Oh write, your IsPostback implementation is incorrect.

That page property is to avoid a piece of code to be executed on every postback. For your case, you need something to be executed on postback, so simply remove it.

Adding a AuotPostback will post the page, execute the change event and the code written in it.
Thomas Daniels 22-Dec-12 6:14am    
Thank you!
Sandeep Mewara 22-Dec-12 6:14am    
Welcome.
Avinash_Pathak 22-Dec-12 6:10am    
have you remove (!IsPostBack)??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900