如何从服务器端显示弹出消息

时间:2009-11-05 13:21:26

标签: asp.net jquery

在asp.net vs05上工作。我知道如何显示弹出窗口,在我的下面的语法中我显示一个页面加载时弹出窗口。但我想在服务器上发生一些操作后显示此弹出窗口,我有一个按钮,在此按钮事件下我想做一些工作在服务器端,然后我想显示弹出消息。这是我可以显示弹出窗口的完整语法

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>

    <script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

       <script type = "text/javascript">

    $(document).ready(function() {
    $("#message").fadeIn("slow");
    $("#message a.close-notify").click(function() {
        $("#message").fadeOut("slow");
        return false;
    });
});
</script>

    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
   <div id='message' style="display: none;">
    <span>Hey, This is my Message.</span>
    <a href="#" class="close-notify">X</a>
</div>

    </form>
</body>
</html>

与上面的语法一样,我希望在某些事件发生后显示来自服务器端的弹出消息。这是我的aspx语法:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>

     <script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

       <script type = "text/javascript">
    function computeCorrectValue(type parameter)
    {

            $(document).ready(function() {
            $("#message").fadeIn("slow");
            $("#message a.close-notify").click(function() {
                $("#message").fadeOut("slow");
                return false;
            });
        });


    }    
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

C#语法:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //want to do some work then want 


        Button1.Attributes.Add("onclick", "javascript:computeCorrectValue(parameter)");
    }
}

帮助我在服务器端发生某些事件后显示弹出消息.popup必须看起来像我的第一个例子。我也可以显示警告消息,但我不想显示警告message.url http://stackoverflow.com/questions/659199/how-to-show-popup-message-like-in-stackoverflow我想要同样的东西,但想在服务器事件后显示。

4 个答案:

答案 0 :(得分:11)

    protected void Button1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(
            this, 
            this.GetType(), 
            "popup", 
            "alert('hello');", 
            true);
    }

答案 1 :(得分:4)

对于将脚本保留在页面模板中的低技术解决方案,您可以将其添加到HTML中(这可以改为$(document).ready()):

<script type="text/javascript">

var missingInfo = '<%= this.MissingInfo %>' == 'True';
if (missingInfo) {
    alert('Not enough information. Please try again.');
}

</script>

并在页面中添加受保护的属性:

private bool missingInfo = false;

protected bool MissingInfo {
    get { return this.missingInfo; }
}

protected void Button1_Click(object sender, EventArgs e)    {
    // Button1 stuff ...
    // ... then if you want to show the alert
    this.missingInfo = true;
}

答案 2 :(得分:3)

string script = string.Format("alert('this is alert');");
        ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "redirect", script, true);

答案 3 :(得分:0)

虽然其他答案中的示例代码正在使用警报,但这只是一个示例。将你的功能放在警告的位置,你就会有弹出窗口。

这是Darin的函数调用代码:

protected void Button1_Click(object sender, EventArgs e)    {
    ScriptManager.RegisterStartupScript(
        this,
        this.GetType(),
        "popup",
        "computeCorrectValue(parameter);",
        true);
}

虽然我真的不确定你在哪里获得传递给你的函数的参数。