Search code examples
asp.nethtmlcontrolsserver-side

How to access data from html controls in asp.net


How do I access data from html in asp.net in the .cs (code behind) file?

In .aspx page I have:

             <tr>
                <td>Username:</td><td><input id="username" type="text" /></td>
             </tr>
             <tr>
                <td>Password:</td><td><input id="password" type="password" /></td>
             </tr>
             <tr>

I know I can convert this to something like:

    <tr>
        <td>Username:</td><td><asp:TextBox ID="username" TextMode="SingleLine" runat="server"></asp:TextBox></td>
     </tr>
     <tr>
        <td>Password:</td><td><asp:TextBox ID="password" TextMode="Password" runat=server></asp:TextBox></td>
     </tr>

This will allow me to access the controls via IDs. However I was wondering if there was a way of accessing data without using asp.net server-side controls.


Solution

  • Give the inputs a name as well as an id and you will be able to get the values from Request.Form. Inputs without names are not sent back with the form post.

    <input id="username" name="username" type="text" />
    <input id="password" name="password" type="password" />
    
    
    var username = Request.Form["username"];
    var password = Request.Form["password"];