Pages

Wednesday, February 23, 2011

Font size changing while loading alert window through Javascript in response.write()

Problem:
I am using javscript to open a alert window when a button is clicked.
Here is the code in cs file :
Response.Write("<script>alert('hai')</script>");

Interesting thing is with the click of button, the font size changed (increased) in the original asp.net page; refreshing the screen brings back the original font size.

I have searched on it google & after that i got solution





Solution
the above problem is caused because the script is coming outside the html in the rendered html page

.Net Frame work has provide a class to manage client side scripts

Try like this.

// Define the name and type of the client scripts on the page.
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(cstype, "name", "<script>alert('hai')</script>");

No comments:

Post a Comment