2

so I have this code within an ASPX page. I am trying to use it within a text box in the same page, but no success. However, I can use the script within a text box in my master page? Any help would be appreciated, thanks

<asp:Content runat="server" ID="FeaturedContent" 
ContentPlaceHolderID="FeaturedContent">

<script src="jquery-1.12.1.js"></script>
<script src="jquery-ui.js"> </script>
<link href="jquery-ui.css" rel="stylesheet" />

<script type="text/javascript">
    $(document).ready(function () {
        $('#txtStationName').autocomplete({
            source: 'StationHandler.ashx'
        });
    });
</script>

 <asp:TextBox ID="txtStationName" runat="server">
 </asp:TextBox>
1
  • 1
    the textbox that generated by the asp is different id, try to look into browser console to check what is the real id, it usually generate like (your masterpagename)_txtstationname or use $('input[id$="txtStationName"]') Commented May 31, 2018 at 3:50

4 Answers 4

5

Since your <asp:TextBox ID="txtStationName" runat="server"> is server side component. .NET will append certain value with your ID. There might be two solution for this condition.

1) You can use ClientIDMode="Static" to make the generated IDs consistent

<asp:Content runat="server" ID="FeaturedContent" 
ContentPlaceHolderID="FeaturedContent">

<script src="jquery-1.12.1.js"></script>
<script src="jquery-ui.js"> </script>
<link href="jquery-ui.css" rel="stylesheet" />

<script type="text/javascript">
    $(document).ready(function () {
        $('#txtStationName').autocomplete({
            source: 'StationHandler.ashx'
        });
    });
</script>

<asp:TextBox ID="txtStationName" runat="server" ClientIDMode="Static">
</asp:TextBox>

2) Or find the exact ID generated on DOM from developer console and use js on that element.

Sign up to request clarification or add additional context in comments.

Comments

2

The ID of your textbox will be changed by the asp.net compiler. To prevent this you'll want to do:

<asp:TextBox ID="txtStationName" runat="server" ClientIdMode="static">

</asp:TextBox>

Comments

1

I suggest you to try with classname in your code

 <asp:TextBox CssClass="uniquetxtStationName" ID="txtStationName" runat="server">
 </asp:TextBox>

and use it in script :

$('.uniquetxtStationName').autocomplete({
    source: 'StationHandler.ashx'
});

Reason behind this : aspx will update all your ID of server tag, so you cant use actual ID of your tag in to javascript function directly.

Comments

0

you have two option to work one is id another is class

<asp:TextBox CssClass="clstext" ID="txtauto" runat="server"> </asp:TextBox>

for class you use

$('.clstext').autocomplete({
source: 'StationHandler.ashx'

});

if you use id then

<asp:TextBox ID="txtStationName" runat="server" ClientIdMode="static">

$('.txtStationName').autocomplete({
source: 'StationHandler.ashx'
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.