ASP.NET中的消息框

时间:2009-12-07 05:46:00

标签: asp.net

如何在内容页面中显示消息框..? 更新个人资料后..我想在内容页面中显示一个消息框..

请提出您的建议..提前致谢。

4 个答案:

答案 0 :(得分:2)

您可以使用Page.RegisterStartupScript方法。

if (UpdateProfile())
    Page.RegisterStartupScript("startup", "<script>alert('your profile has been updated..');</script>");

当然假设UpdateProfile()完成工作并返回一个表示成功的布尔值:)

或者(因为该方法已过时),您可以使用ClientScriptManager.RegisterStartupScript方法。

if (UpdateProfile())
    Page.ClientScript.RegisterStartupScript(this.GetType(), "startup", "<script>alert('your profile has been updated..');</script>", false);

答案 1 :(得分:1)

首先编写此方法

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

在您需要消息框后,只需按此行

MsgBox("Your Message!!!", this.Page, this);

答案 2 :(得分:0)

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

stackoverflow不允许真实标签替换[with&lt;和&gt;

答案 3 :(得分:0)

您看到的错误是由于您的内容页面以某种方式尝试注入javascript以在Content控件之外创建警告框。

这样做的一种方法是在主页面级别注入javascript。

为此,请在主页面代码中公开一个方法,如下所示:

public void ShowAlertMessage(String message)
{
    string alertScript = String.Format("<Script Language='javascript'> alert('{0}');</script>", message);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Key", alertScript, false);            
}

然后,您可以从内容页面在Master对象上调用此方法:

protected void UpdateProfile_Click(object sender, EventArgs e)
{
    YourMasterPage master = (YourMasterPage) Master;
    master.ShowMessage("Profile updated.");
}

此方法还具有为所有内容页面封装MessageBox逻辑的好处。


上面的一个警告是,我不能为我的生活重现你所看到的错误,我已经尝试过我能想到的主/内容标记的每一个组合,并且无法得到错误。在其他答案中提供的任何其他示例对我来说都很愉快。