简体   繁体   中英

Can't get data from textbox

I'm trying to get data from textbox but it says undefined id or something like that. Here is my code. I didn't understand what the problem is. Text1, Text2 and Text3 are my id of textboxes.

  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 part

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

You need to add runat="server" to the input elements that you want to access with your server code.

Example

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

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

Alternatively, you can use the server control asp:Textbox .

使TextBox成为服务器端控件。

Why don't you use the asp-controls?

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

In your code-behind, you can access it and set the text with:

txtExample.Text = "Test";

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