开发者

ASP.net checkbox always checked

开发者 https://www.devze.com 2023-01-18 17:43 出处:网络
<asp:CheckBox ID=\"isSubscribed\" runat=\"server\" /> Subscribe to mailing list?<br /><br />
<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 fi开发者_如何学JAVAres 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?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号