How to get Request.Form["elementName"] of an element while it has the attribute runat="server"?

8.3k Views Asked by At

So, I have an element:

<input id="loginEmail" type="email" class="form-control" name="email" placeholder="email" />

I can get it using Request.Form["email"]

While when I use the following:

<input id="loginEmail" runat="server" type="email" class="form-control" name="email" placeholder="email" />

Note: I added runat="server" it doesn't work. How can I retrieve the data without using loginEmail.Value?

The reason I want to do this is to preserve the input value after the method is run, like so(should the submission not work out and I show an error message for the user to edit the values of the form):

loginEmail.Value = Request.Form["email"];

Thank you all.

2

There are 2 best solutions below

0
On BEST ANSWER

If it's not a server control you select your input control's value by name. If it's a server control you need to select it by Id.

Request.Form["loginEmail"]

Use Request.Form.AllKeys to check what's sent and how it's sent.

1
On

Although @Schadensbegrenzer answer might be of your use but conceptually it is wrong. Request.Form will only give you value if name is passed.

Mentioned answer is working for you because ASP.Net will automatically add name attribute with the value same as id if you don't specify it. So here id = name and you are picking is based on name only.

You can just have name that is different from id with runat = server and try to pick value based on id, it will give you null.

The answer is you question is "It is impossible".