Crazy Programmer
Crazy Programmer

Reputation: 196

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#?

Upvotes: 0

Views: 82

Answers (2)

bastos.sergio
bastos.sergio

Reputation: 6764

Add this (in your C# method)

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

    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Test');", true);
}

Upvotes: 1

Amit Joki
Amit Joki

Reputation: 59292

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

Upvotes: 1

Related Questions