4

How can I read a HTML Element like input type=checkbox from the code behind? Without adding runat="server". Is that possible?

4
  • 1
    Please explain what do you want to do. I think you want to read rendered html. Right? Commented Nov 13, 2009 at 3:04
  • i have a input type=checkbox in my page and i want to know if it's checked or not from code behind. i dont want to use asp.net checkbox or add a runat=server to it. is that possible? Commented Nov 13, 2009 at 3:08
  • Yes, they're rendered as html literals and you can access them by using the Controls object. Commented Nov 13, 2009 at 3:09
  • can you give me an example? will appreciate it. tnx Commented Nov 13, 2009 at 3:11

2 Answers 2

6

How can i read a HTML Element like input type=checkbox from the code behind?

Before asking this you should first understand that HTML Elements have nothing to do with the code-behind (on the server).
You cannot access them in any way as you cannot access the client's browser from the server.

If you want to access THE VALUE of the INPUT element that is posted to the server then you should use:

var postedValue = Request.Form["nameOfElement"];

So if you have HTML:

<input type="check" name="nameOfElement" value="Yes" />

then:

  • If user checked the element, then you will get "Yes" in the postedValue.
  • Otherwise postedValue will be null.
Sign up to request clarification or add additional context in comments.

Comments

3

Look in the Request.Form collection to retrive those values

something along the lines of checking for this condition:

Request.Form("Name") != null

to check if the checkbox of name "Name" is checked. If the box isn't checked that value will be null

1 Comment

It is not an ID of element, but its NAME.

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.