c# - Get value from input box in code behind - Stack Overflow

This the code in my .aspx page and I am using auto plete for textbox.<script type="textjavascr

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

<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autoplete({
            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 plete 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: .html

Thanks guys for your help.

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

<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autoplete({
            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 plete 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./2013/02/get-html-input-controls-value-server.html

Thanks guys for your help.

Share Improve this question edited Oct 8, 2013 at 13:44 sudhansu63 6,2104 gold badges40 silver badges54 bronze badges asked Oct 8, 2013 at 9:05 user2345661user2345661 4253 gold badges8 silver badges14 bronze badges 2
  • Do you want to cal it in a page load? and check in console are you getting any error? – Just code Commented Oct 8, 2013 at 9:30
  • You are performing an ajax operation. This will not re-render your entire page. You will need to load the response values to the control. – Kami Commented Oct 8, 2013 at 9:40
Add a ment  | 

2 Answers 2

Reset to default 2

If you are using MasterPage then ASP 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%>").autoplete({

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745552755a4632652.html

相关推荐

  • c# - Get value from input box in code behind - Stack Overflow

    This the code in my .aspx page and I am using auto plete for textbox.<script type="textjavascr

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信