0

How can I get the values from checkboxes that are added by JavaScript, regardless whether the checkboxes are checked or not?

These checkboxes are created by JavaScript client side, their values will represent some data which will be saved to the database. That's why I need to get at them server side.

4
  • Are they a CheckBoxList? If not, are they contained within another control? You do not have to pass the values via the URL... Commented Mar 15, 2012 at 22:33
  • @IrishChieftain at least I need to get the checkbox ids to check Checked property... this is the same as how to get their values... Commented Mar 16, 2012 at 13:59
  • 1
    No JS expert but there is a page property in 4.0 that lets you get the client id server side without anything being appended to it. Commented Mar 16, 2012 at 14:06
  • @IrishChieftain it looks like Page.Controls can return all the controls... have not done all the details yet... will see... thanks Commented Mar 16, 2012 at 14:17

1 Answer 1

4

You need to make sure that these checkboxes, when created on the client side with Javascript, have a name attribute assigned so that they are submitted in the Request.

For example:

<input type="checkbox" id="mycheck" name="mycheck" />

When the form is submitted, you can access the checkbox by looking at the request params as so:

//will probably say "on" or "checked", not sure.
string mycheckbox= HttpContext.Current.Request.Params["mycheck"];

Note that you will only see in the request parameters, the checkboxes that are checked off. If you create 5 checkboxes and only one is checked off, only the one checked off will be present in the Params collection

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

8 Comments

...my question is to get ALL checkbox values... not only those that are checked off
I thought I had been clear... You won't get ALL checkboxes, you will only get the ones that are checked off. This is logical because it wouldn't make sense to submit a request with 1000 params just because you have 1000 controls on a page. But finding a workaround is trivial: put in a hidden field the total number of checkboxes that you have created so far and name them sequentially. If the hidden field has a value of 20 and you only get in the params collection checkbox5, you can assume that all other 19 boxes were not checked off.
@shrimprice There are many ways to tackle this if you put a little bit of effort into thinking about the problem.
@shrimprice You can't loop through Page.Controls because they won't be there; as you explained, they are created on the client-side using Javascript. Page.Controls will only hold server-side controls (those that are declared with runat="server"). Again, there are many ways tackle this, I presented one that solves your problem. It's not the only way but it's one way. Your English is not the problem, yours is as good as mine
@IrishChieftain because these checkboxes are created on the client-side using Javascript. You won't have an instance of a Checkbox object in the code behind so that you can say mycheckBox.Checked. Your code won't even compile. The only way is to access the Request.Params object.
|

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.