【问题标题】:How to read user input from .aspx and store it in SQL Server database in .aspx.cs如何从 .aspx 读取用户输入并将其存储在 .aspx.cs 中的 SQL Server 数据库中
【发布时间】:2019-02-22 20:17:48
【问题描述】:

我是 asp.net 编程的新手,我还在学习,现在我正在构建一个登录表单。

这是我的 aspx(HTML 文件):

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

我需要在 aspx.cs 中创建一个按钮单击事件以将数据存储在 SQL Server 中,在我的注册表单中我这样做了:

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

不同的是.aspx注册文件中的TAG是这样的:

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

如何在不使用这些 asp 标签的情况下做我在注册表中所做的相同操作?

【问题讨论】:

  • 如果您要使用 Web 表单,为什么要避免使用 Web 控件?

标签: c# html css asp.net sql-server


【解决方案1】:

您正试图摆脱 ASP.NET 标记控件,这些控件实际上是 HTML 等价物周围的语法糖。你可以重写你的 ASP 文本框标签如下:

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

runat="server" 属性使您可以灵活地在代码隐藏 (aspx.cs) 中访问此控件,如果您不打算这样做,也可以将其删除。

【讨论】:

  • 我理解@Pvxtotal 问题的方式是他想消除“ASP.NET”标签,这相当于他在问题中的内容
  • 问题是不使用asp标签,无法使用.Text,有没有别的办法?
  • 您也可以在服务器端访问innerText 属性。所以你会做类似nome..Attributes("innerText")
【解决方案2】:

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

string userName = user.Value;

【讨论】:

    【解决方案3】:

    您可以通过使用该控件的名称直接访问控件的值

    你的情况

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

    您可以在 .cs 文件中访问此值,例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多