简体   繁体   中英

How to read user input from .aspx and store it in SQL Server database in .aspx.cs

I'm new to programming in asp.net, I'm still learning and right now I'm building a login form.

This is my aspx (HTML file):

<div class="login">
    <input type="text" placeholder="User" name="user" id="user" runat="server"><br>
    <input type="password" placeholder="Password" name="password" id="password" runat="server"><br>
    <button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click">Login</button>
</div>

I need to create a button click event in aspx.cs to store the data in SQL Server, in my registration form I did this:

cmd.Parameters.AddWithValue("user", user.Text);
cmd.Parameters.AddWithValue("password", password.Text);

The difference was that the TAG in the .aspx file for registration was like this:

<td class="auto-style8">  
    <asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>  
</td>  

How can I do the same that I did in the registration form without using these asp tags?

You are trying to get rid of ASP.NET tag controls which effectively are syntactic sugar around HTML equivalents. You could re-write you ASP Textbox tag as below:

<input type="text" ID="nome" runat="server" style="width:159px"></input>

The runat="server" attribute gives you the flexibility to access this control at code behind (aspx.cs) and if you do not intend to do that, it can be removed as well.

从后面的代码访问像这样的文本控件的值-

string userName = user.Value;

you can directly access value of controls by using name of that control

In your case

 <td class="auto-style8">  
        <asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>  
 </td>  

you can access this value in .cs file like

cmd.Parameters.AddWithValue("user", nome.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM