Jepe d Hepe
Jepe d Hepe

Reputation: 909

Read HTML Elements from Code Behind using asp.net C#

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

Upvotes: 4

Views: 18986

Answers (2)

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24078

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.

Upvotes: 6

gn22
gn22

Reputation: 2086

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

Upvotes: 3

Related Questions