user2345661
user2345661

Reputation: 425

Get value from input box in code behind

This the code in my .aspx page and I am using auto complete for textbox.

<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Preferences.aspx/GetAutoCompleteData",
                    data: "{'Name':'" + document.getElementById('txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }
</script>


<table style="width:auto">
    <tr>
        <td>
            <asp:Label ID="Label1" runat="server" Text="Please Type Student Name:"></asp:Label>
        </td>
        <td>
            <input type="text" id="txtSearch" class="autosuggest" />
            <asp:TextBox ID="TextBox1" runat="server" Text=txtsearch Visible="true" class="autosuggest"></asp:TextBox>
        </td>
        <td>
            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
        </td>
    </tr>
</table>

If I put runat="Server" in

<input type="text" id="txtSearch" class="autosuggest" />

then auto complete doesn't work. Form tag is used in master page.

Problem Solved using the name property of control

Source Code:

    <input type="text" name="_name"  />

    <asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" />

CodeBehind:

protected void btnShow_Click(object sender, EventArgs e)
 {
    string strValue = Page.Request.Form["_name"].ToString();
    Response.Write(strValue);
 }

Reference: http://www.etechpulse.com/2013/02/get-html-input-controls-value-server.html

Thanks guys for your help.

Upvotes: 1

Views: 10399

Answers (2)

sudhansu63
sudhansu63

Reputation: 6180

For getting value form text box, you need to use server control.

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

you can also add ClientIDMode= "static" in the server control to retain your id exactly same or you can use $("#<%=txtSearch.ClientID%>") to get the dynamicaly generated Id.

Upvotes: 0

Zaki
Zaki

Reputation: 5600

If you are using MasterPage then ASP.net will auto generate a name for each control and appends it to your control, In order to get the unique ID of the control you need to use JQuery ClientID.

Put runat="server" but change your jquery function to (this will retrieve the input by ClientID):

 function SearchText() {
        $("#<%=txtSearch.ClientID%>").autocomplete({

Or as an alternative(suggested by DeeMac) use static ClientIDMode.

Upvotes: 2

Related Questions