You must Sign In to post a response.
  • Category: JavaScript

    How to javascript function from dropdownlist?

    I have javscript function which I want to call from dropdownlist.If I call javascript function from onchange event of dropdownlist from page_load,my dropdownlist's selectedIndexchanged event does not fire.How to handle it?.I want to call both javascript function and selectedindexchanged from dropdownlist.

    <script language="Javascript" type="text/javascript">

    ddlSubject.Attributes.Add("onchange", "return CallSubject()");

    protected void ddlSubject_SelectedIndexChanged(object sender, EventArgs e)

    Suppose I have button control and want call javascript function from button.I will call javascript function from button's onclientclick event,after that my button's click event fire.I want like that in dropdownlist.
  • #765329
    You need to set :
    for the drop down.

    It will surely work.

    Anjali Bansal

    ~Give your best and lead the world

  • #765331
    <select id="myList" >
    <option id="stylesheet1" value="default">Default</option>
    <option id="stylesheet2" value="one">Theme 1</option>
    <option id="stylesheet3" value="two">Theme 2</option>
    <option id="stylesheet4" value="three">Theme 3</option>

    document.getElementById("myList").onchange = function() {
    var sheet=document.getElementById("myList").value;

    else if(sheet=="two"){
    else if(sheet=="three"){
    return false

  • #765334
    Add this line inside Page_Load():
    ClientScript.GetPostBackEventReference(this, string.Empty);
    ASPX Page:
    function abc()
    __doPostBack('<%= YourDropDown.ClientID %>', '');
    <asp:DropDownList runat="server" ID="YourDropDown" onchange="javascript:return abc();" OnSelectedIndexChanged="Unnamed_SelectedIndexChanged"></asp:DropDownList>
    Hope it helps.
    Shashikant Gurav

  • #765752

    by the code ddlSubject.Attributes.Add("onchange", "return CallSubject()"); you are assigning event to ddlSubject dropdown but you are not calling the function .

    to call a script inside server side function we need server startup in out code then only we can call client side script inside the server side .

    Eg definition of script
    function CallSubject() {

    inside onload of server side or in any event call we need to code like this to call client side function

    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "CallSubject()", true);

    ScriptManager.RegisterStartupScript -- > which use to call the client side function in server side
    "text" is the name you can give any name for that script we mention
    CallSubject() : where we call our exact client side function

  • #765764
    onchange event is from client side and on_selected index change is from server side, first onchange event gets triggered if it returns TRUE then it automatically call selectedIndexChanged otherwise it won't.
    basically you need to keep 'autopostback' property of dropdown list set to 'true'

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • Sign In to post your comments