Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a javascript function from Code-behind...

Status
Not open for further replies.

brucegonewild

Programmer
Jan 25, 2008
22
CA
How can I call a javaScript function that I've registered through this.Page.RegisterClientScript("key", scriptText)?

To be more precise, I need to pop up an error message (an alert) when a SystemException occurs in my code behind.

Ideas?

cheers!
 
Here's a lazy way of doing it:

response.write("<script language=javascript>alert('Error Message Here!');</script>")

HTH
 
Here's a slightly less lazy way of doing it:

Code:
public static void CreateMsgBox(System.Web.UI.Page aspxPage, string strMessage, string strKey)
{
	string strScript = "<script language = JavaScript>alert('" + strMessage + "')</script>";
	if (!aspxPage.ClientScript.IsStartupScriptRegistered(strKey))
	{
		aspxPage.ClientScript.RegisterStartupScript(aspxPage.GetType(), strKey, strScript);
	}
}

I use this in a "utilities" class on my website that I can call from any page. But the key is to use RegisterStartupScript.

HOpe this helps,

Alex

[small]----signature below----[/small]
I'm pushing an elephant up the stairs

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top