views:

801

answers:

4

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.

+3  A: 

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"];
tvanfosson
A: 

Add runat="server" to the controls, and then you can access them from the code-behind almost as if they were <asp:______ /> controls.

Joel Coehoorn
A: 

asp.net controls, are in essence html controls wrapped, so an asp:Button will render as a input html control. Some web developers prefer using html controls due to the smaller size. Therefore each html control will map to a asp server control. As the previous answer, from Joel, add the runat="server", then the control can be referenced by the ID in the code behind

Stuart
A: 

Mr. ran170

this is u r code

Username: Password:

you just add the runat="server" attribute to the html controls like

Username: Password:

so you can access the controls in asp.net

Surya sasidhar