简体   繁体   中英

how to write javascript validation for dropdown selected index changed event in c#

I have a dropdown.Basing on this dropdown i shud load another dropdown.i wrote code in on selected index changed event in .cs page.Depending on selectd item in first dropdown i need to give a validation in javascript.let us think i should show an alert.Can any one help me out.

in .aspx page

<asp:DropDownList ID="drpdes" runat="server" onselectedindexchanged="drpdes_SelectedIndexChanged"></asp:DropDownList>

in .cs page

protected void Page_Load(object sender, EventArgs e)
    {
         drpdes.AutoPostBack = true;
    }

 protected void drpdes_SelectedIndexChanged(object sender, EventArgs e)
    {
         int flgchk = 0;
          if(drpdes.selectedvalue == "0")
           {
                flgchk = 1;
           }
        // my code for binding second dropdown
    }

when flgchk = 1 i need to show an alert from javascript.i wrote a function for alert in javascript.But i am not understanding where to call it.

something like this :)

protected void drpdes_SelectedIndexChanged(object sender, EventArgs e)
    {
         int flgchk = 0;
          if(drpdes.selectedvalue == "0")
           {
                flgchk = 1;
              /// this will call your alert method. 
                string errorScript = "<script type=\"text/javascript\">" + 
                      "YourFunctionNameHere() " + 
                      "</script>";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", errorScript);
           }
        // my code for binding second dropdown
    }

If you remove AutoPostback on your dropdownlist you can validate client side. Something like onchange = YourJSMethod(this.value).

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