0

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 suggestions.. Thanks in advance.

Siva Kumar
  • 164
  • 1
  • 2
  • 9

4 Answers4

2

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);
Damovisa
  • 19,213
  • 14
  • 66
  • 88
  • yup you can use execute any javascript by supplying the function name or the script and if you dont have any specific reason why not display message on the page itself instead of "alert()". – Vinay Pandey Dec 07 '09 at 06:25
  • This works as well, I was going for simplest thing, this is more correct in a master page. Not sure what happened with the script tags but they were eaten when I did preview. – DevelopingChris Dec 07 '09 at 14:51
1

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);
Harsh Varudkar
  • 181
  • 1
  • 4
0
 Response.Write("[script] alert('message here');[/script]");

stackoverflow won't allow the real tags replace the [ with < and the ] with >

DevelopingChris
  • 39,797
  • 30
  • 87
  • 118
  • 1
    If i Use the script tag within Response.write method within content page of master page..it shows the error "Only Content controls are allowed directly in a content page that contains Content controls." – Siva Kumar Dec 07 '09 at 05:53
0

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.

David Hall
  • 32,624
  • 10
  • 90
  • 127