0

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

2
  • 1
    It should work. Check this stackoverflow.com/questions/12517227/… may be useful Commented Apr 1, 2014 at 11:28
  • This would call javacript as soon as the dropdown changes... I want to call javaScript after few actions are performed.... Commented Apr 1, 2014 at 11:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Add this (in your C# method)

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.