简体   繁体   中英

Calling the onclient click event of a button when dropdownlist index is changed

I have a dropdownlist. When an item is selected in the dropdownlist, i am populating some fields in the onselectedindexchanged event. I need to use these values and show some javscript alerts and load the page again with the new values. But when i tried using onchange and onselectedindexchanged events of dropdownlist, it obviously first calls the javascript onchange event and i am not able to get the populated values.

So i tried to use the onclientclick event of an invisible button. And i need to call this from onselected indexchanged event of dropdownlist.

Can anyone please suggest how to do this? Or provide some better solution to my problem.

Here is my JS function

  function changeEnvironment() {
        var permission = document.getElementById("hiddenPermissionFlag").value;      
        if (permission == "true") {
            var newEnvironment = document.getElementById('<%=ddlChangeEnvironment.ClientID %>').value;
            alert("Entering  " + newEnvironment + " environment");
        }
        else if (permission == "false") {
            var environment = document.getElementById("hiddenEnvironment").value;                 
            alert("Not required permissions. You are in " + environment + " environment");
        }
        return true;        
    }

All the HiddenFields are populated in onselectedindexchanged event in code behind file

You can use ScriptManager.RegisterStartupScript to call javascript function from code behind.

Write below line in code behind on onselectedindexchanged event of dropdown list. This should display alert message as written in below code. You can execute your js function as required.

ScriptManager.RegisterStartupScript(this, typeof(string), "KEY", "javascript:alert('Alert Called');", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM