简体   繁体   中英

ASP call javaScript from c#

In my code I have used a dropdown onSelectedIndexChanged event and few things happen... I want to call JavaScript after that.... I have tried using

dropdown.Attributes.Add("onchange", "javascript:alert('Test');"); 

the above code does not fire

and

dropdown.Attributes.Add("onblur", "javascript:alert('Test');");

this is also not useful as the dropdown is autopostback and it loses focus because of that

Is there any way through which I can call JavaScript function through c#?

Your question was: Is there any way through whihc I can call Javascript function through c#

To answer that:

You can use two methods:

ClientScript.RegisterClientScriptBlock(this.GetType(), "anyUniqueName", "script here", true);
ClientScript.RegisterStartupScript(this.GetType(), "anyUniqueName", "script here", true);

1st method just puts a block, while the second one puts the tag at the bottom of the page

Add this (in your C# method)

protected void onSelectedIndexChanged(Object sender, EventArgs e)
{
    //do stuff
    ...

    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Test');", 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