locked
C# control event handling with javascript RRS feed

  • Question

  • How can I get javascript to execute for "onchange" / "OnSelectedIndexChanged" event instead of a CodeBehind method? I think "OnSelectedIndexChanged" event has to be handled by CodeBehind; but how can I replace that (or onchange) with a javascript event handler?

    I have the following in my xyz.ascx file:

    <asp:DropDownList ID="ddlTypeCar" CssClass="BatsRefAddressTypes" runat="server"
                      onchange="javascript: testAlert();"
                      OnSelectedIndexChanged="ddlTypeCar_SelectedIndexChanged"
                      AutoPostBack="True" />

    I could not get the javascript to execute, even though I removed OnSelectedIndexChanged="ddlTypeCar_SelectedIndexChanged" and/or set AutoPostBack="false".

    Additionally, I tried added the following in xyz.ascx.cs Page_Load() method.

    ddlTypeOfAddress.Attributes.Add("onchange", "javascript: testAlert();");

    which didn't help.

    Thanks for your help in advance.



    • Edited by ZAT123 Wednesday, October 1, 2014 6:35 PM
    • Moved by Caillen Thursday, October 2, 2014 7:55 AM
    Wednesday, October 1, 2014 6:30 PM

Answers

All replies

  • Hi,

    you can control client events instead of server events with javascript

    look this url for more information

    http://www.w3schools.com/js/js_events.asp

    Regards

    Wednesday, October 1, 2014 6:36 PM
  • Please post questions related to ASP.NET in the ASP.NET forums (http://forums.asp.net ).
    • Proposed as answer by Caillen Thursday, October 2, 2014 7:54 AM
    • Marked as answer by Just Karl Friday, October 17, 2014 9:16 PM
    Wednesday, October 1, 2014 9:21 PM