【问题标题】:Can't get data from textbox无法从文本框中获取数据
【发布时间】:2012-05-09 10:01:42
【问题描述】:

我正在尝试从文本框中获取数据,但它显示未定义的 id 或类似的东西。这是我的代码。我不明白问题是什么。 Text1、Text2 和 Text3 是我的文本框 ID。

  SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
     DataSet thisDataSet = new DataSet();
     thisAdapter.Fill(thisDataSet, "Odunc");
     DataRow thisRow = thisDataSet.Tables["Odunc"].NewRow();

    thisRow["Book_Name"] = "" + Text1.Text;
    thisRow["Reader_Name"] = "" + Text2.Text;
    thisRow["Expiration_Date"] = "" + Text3.Text;
     thisDataSet.Tables["Odunc"].Rows.Add(thisRow);
     thisAdapter.Update(thisDataSet, "Odunc");

asp 部分

   <table style="width:100%;">
    <tr>
        <td class="style1">
            Name of Reader</td>
        <td>
    <input id="Text1" name="Text1" type="text" /></td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style1">
            Name of Book</td>
        <td>
    <input id="Text2"  name="Text2" type="text" /></td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style1">
            Expiration Date</td>
        <td>
    <input id="Text3"  name="Text3" type="text" /></td>
        <td>
            &nbsp;</td>
    </tr>
</table>

【问题讨论】:

  • 如果它们不是服务器控件,您需要从表单集合中获取值

标签: c# asp.net


【解决方案1】:

您需要将runat="server" 添加到您要使用服务器代码访问的输入元素中。

示例

<%-- markup --%>
<input runat="server" id="Text1" name="Text1" type="text" />

// server code
string value = this.Text1.Value; // not ".Text"

或者,您可以使用服务器控件asp:Textbox

【讨论】:

    【解决方案2】:

    使 TextBox 成为服务器端控件。

    【讨论】:

      【解决方案3】:

      为什么不使用 asp 控件?

      <asp:TextBox ID="txtExample" runat="server" />
      

      在您的代码隐藏中,您可以访问它并设置文本:

      txtExample.Text = "Test";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-15
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多