5
<asp:CheckBox ID="isSubscribed" runat="server" /> Subscribe to mailing list?<br /><br />
<asp:Button runat="server" CssClass="btn" ID="btnPrefUpdate" OnClick="updatePrefs" Text="Update" /><br /><br />

This fires in the code behind:

protected void updatePrefs(object sender, EventArgs e)
{
    Response.Write(isSubscribed.Checked);
    Response.End();
}

But it's always coming out as true! Whether it is checked or not! I know I'm doing it wrong, could someone show me how to access this value properly?

3
  • Do you have anything in your Page_Load event which ever sets it as true? Have you tried putting "isSubscribed.Checked.ToString"? Commented Sep 30, 2010 at 10:58
  • Ah yes, in my page load I have isSubscribed.Checked = Master.isSubscribed; how do I get the on click event to run before that one? Commented Sep 30, 2010 at 11:03
  • as a side note I think Respose.Write is evil and should be avoided if possible. It can make debugging your code a nightmare :( Commented Sep 30, 2010 at 11:13

2 Answers 2

9

like @Curt said, it looks to me like you have something in your page_load. if you set the value in Page_Load make sure it is inside the following if statement

if(!Page.isPostBack)
{
    isSubscribed.Checked = true; 
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing it the right way. The boolean property Checked should just say True or False (I even tested it). Is your Page_Load doing something with the checkbox? In other words, is the value of the checkbox somehow (re)set when the post back is occuring (the postback of the button click).

In your Page_Load method you could include:

if (!this.IsPostBack)
{
// Set default or loaded values for controls
}

3 Comments

Yes, stupidly it sets it to the profile value in page load, how can I stop that code from loading before the button click event?
@Tom Gullen see my answer, or you could set it in the aspx page
if (!this.IsPostBack) // Do your thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.