Siva Kumar
Siva Kumar

Reputation: 164

Message Box in ASP.NET

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.

Upvotes: 0

Views: 1591

Answers (4)

Harsh Varudkar
Harsh Varudkar

Reputation: 191

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);

Upvotes: 1

David Hall
David Hall

Reputation: 33183

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.

Upvotes: 0

Damovisa
Damovisa

Reputation: 19423

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);

Upvotes: 2

DevelopingChris
DevelopingChris

Reputation: 40808

 Response.Write("[script] alert('message here');[/script]");

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

Upvotes: 0

Related Questions