0

I want to call javascript function on Dropdown menu selectedindexchanged. I tried this

<asp:DropDownList ID="selectVehicle" AutoPostBack="true" OnSelectedIndexChanged="GetRoute(this.options[this.selectedIndex].value);" runat="server" CssClass="inners">
<asp:listitem Selected>-- Select Vehicle --</asp:listitem>
<asp:listitem Value="16">Tata Ace</asp:listitem>
<asp:listitem Value="28">Tata 407</asp:listitem>
</asp:DropDownList>

Error

BC30456: 'GetRoute' is not a member of 'ASP.index3_aspx'.

Then I tried onChange instead of OnSelectedIndexChanged but it has no use for me since in my js function values inserted in textbox which is working but when page reloads textbox again gets blank & I can't turn off AutoPostBack since it is required to postback. Is is possible I can run javascript OnSelectedIndexChanged ?

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
SuRaj Creator
  • 945
  • 1
  • 9
  • 25
  • No, `OnSelectedIndexChanged ` is a server side event handler you can't attach a client side handler to it. What you are trying to do exactly? – Rahul Singh Nov 25 '15 at 06:45
  • @RahulSingh on dropdownselection I want to call GetRoute(); script which calculates distance between two places & put it on textbox. If I use OnChange then after postback textbox gets blank again. I just want distace value to keep in textbox after postback. – SuRaj Creator Nov 25 '15 at 06:48
  • @RahulSingh Can I put GetRoute() function in code behind then might OnSelected IndexChanged might work for me? If it is possible then can you tell me how to do it? – SuRaj Creator Nov 25 '15 at 06:53
  • Yes if you are anyways doing postback then write the logic in dropdown change event handler in code behind. – Rahul Singh Nov 25 '15 at 06:56

2 Answers2

0

You can use the standard javascript onchange:

<asp:DropDownList ID="selectVehicle" AutoPostBack="false" onchange="GetRoute(this.options[this.selectedIndex].value);" runat="server" CssClass="inners">
</asp:DropDownList>

Assuming GetRoute is a javascript function.

ASP.NET will ignore the onchange attribute and render it in the resulting select element.

Shaun
  • 933
  • 9
  • 16
  • Thanks for reply but This I already done but I want autopostback="true" I need to keep AutoPostBack enable. If I keep it enable then Textbox lose it's value which is calculated by GetRoute function. Is there anyway to keep that value after postback? – SuRaj Creator Nov 25 '15 at 07:25
  • If you have postback true it will run the onchange you have defined then the onselectedindexchange server side function. Is the textbox an asp.net textbox ? – Shaun Nov 25 '15 at 07:30
  • Are you using traditional full postback or Ajax postback / script manager ? – Shaun Nov 25 '15 at 07:32
0

Rahul Singh's comment to write the JS code into the server code, and only run the server code, no Javascript, makes sense.

In the event you have to have the JS for some reason, you can remove the autopostback = true, and manually call __doPostBack with parameters, the value your JS is producing for example.

JavaScript: Multiple parameters in __doPostBack

Community
  • 1
  • 1
llc381
  • 101
  • 2
  • 10