Search code examples
c#asp.netrunatserverrequest.form

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


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.


Solution

  • 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.