How to display the message Box within the Content Page..? After updating profile..I want to display a Message Box in content page..
Please give your sugg开发者_如何转开发estions.. Thanks in advance.
You could use the Page.RegisterStartupScript
method.
if (UpdateProfile())
Page.RegisterStartupScript("startup", "<script>alert('your profile has been updated..');</script>");
Assuming of course that UpdateProfile() does the work and returns a boolean indicating success :)
Alternatively (because that method is obsolete), you could use the ClientScriptManager.RegisterStartupScript
method instead.
if (UpdateProfile())
Page.ClientScript.RegisterStartupScript(this.GetType(), "startup", "<script>alert('your profile has been updated..');</script>", false);
write this method first
public void MsgBox(String ex, Page pg,Object obj)
{
string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
Type cstype = obj.GetType();
ClientScriptManager cs = pg.ClientScript;
cs.RegisterClientScriptBlock(cstype, s, s.ToString());
}
after whenever you need message box just follow this line
MsgBox("Your Message!!!", this.Page, this);
The error you are seeing is caused by your content page somehow trying to inject the javascript to create the alert box outside of the Content control.
One way of doing this that should work is to inject the javascript at the master page level.
To do this expose a method in you master page code behing like the following:
public void ShowAlertMessage(String message)
{
string alertScript = String.Format("<Script Language='javascript'> alert('{0}');</script>", message);
Page.ClientScript.RegisterStartupScript(this.GetType(), "Key", alertScript, false);
}
Then, from the content page you can call this method on the Master object:
protected void UpdateProfile_Click(object sender, EventArgs e)
{
YourMasterPage master = (YourMasterPage) Master;
master.ShowMessage("Profile updated.");
}
This method also has the nice benefit of encapsulating your MessageBox logic for all your content pages.
One caveat on the above is that I can't for the life of me reproduce the error you are seeing, I've tried every combination of master/content markup I can think of and can't get the error. Any of the other examples provided here in the other answers work happily for me.
Response.Write("[script] alert('message here');[/script]");
stackoverflow won't allow the real tags replace the [ with < and the ] with >
精彩评论