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.

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>");
This code will help us to resolve our font size increase issue.


Happy Coding!!

No comments:

Post a Comment