1

I'm using an html file like below (tv.html):

<form action="myaspx.aspx?appid=5018" method="post" id="formSubscription" name="formSubscription">
<input type="checkbox" id="chckIsConfirm" name="chckIsConfirm"/> I Confirm
            <input type="submit" value="Ok" />
</form>

And trying to get it from "myaspx.aspx". And the code behind is:

protected void Page_Load(object sender, EventArgs e)
{
            if (!IsPostBack)
            {
               var r = HttpContext.Current.Request["chckIsConfirm"];
                 //Or
                NameValueCollection nvc = Request.Form;
               //Or
              var a = Request.Form.AllKeys;
            }
}

But I cannot pass the value of "chckIsConfirm". All keys comes empty.. What am I doing wrong?

UPDATE: Only when I check the checkbox it comes as "ON" other times just NULL.

Thanks in advance

3 Answers 3

3

That's the default behavior for checkboxes in most browsers. If it's not checked the browser will not send it with the request. As a workaround add a hidden field that has the same name as the checkbox and value "0", "OFF", "FALSE" or whatever you want the "unchecked" value to be.

Sign up to request clarification or add additional context in comments.

Comments

0

You are missing runat="server"

aspx

<input type="checkbox" id="chckIsConfirm" runat="server" name="chckIsConfirm"/>

cs

var checked= chckIsConfirm.Checked;

7 Comments

runt="server" is not necessary to simply post to an ASPX page from a non-aspx page.
No I know that. But it is much easier to get the value if you have.
I guess "easier" would depend on the actual usage scenario (same/different servers etc.)
Yeah that depends on the scenario right. But using a hidden filed for a work around instead of having runat="server". Is that a god solution?
Of course it is in this situation (and is fairly common). If I have an .html page (which he indicated he did) just adding runat="server" won't really do anything. You would first have to convert the entire page to regular ASP.NET webform and then adding runat="server" would have an effect.
|
0

From your posted code, you are checking them only on first request, but when you click the button it is a postback so you are skiping the read of values

Comments

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.