jQuery 如何从服务器端显示弹出消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1680542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show popup message from server side
提问by HelloBD
work on asp.net vs05. i know how to show popup ,in my below syntax i show a popup when a page is load.But i want to show this popup after some action occur on server ,i have a button ,under this button event i want to do some work on server side ,then i want to show popup message .Here is my complete syntax that can show popup
在 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>
Like the above syntax i want to show popup message from server side after some event occur .Here is my aspx syntax:
像上面的语法一样,我想在某些事件发生后从服务器端显示弹出消息。这是我的 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# syntax :
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)");
}
}
Help me to show popup message after some event occur on server side.popup must look like my give first example .i can show alert message too ,but i don't want to show alert message.url http://stackoverflow.com/questions/659199/how-to-show-popup-message-like-in-stackoverflow
i want same thing but want to show after server event.
帮助我在服务器端发生某些事件后显示弹出消息。弹出必须看起来像我给出的第一个示例。我也可以显示警报消息,但我不想显示警报消息。urlhttp://stackoverflow.com/questions/659199/how-to-show-popup-message-like-in-stackoverflow
我想要同样的东西但想显示服务器事件后。
采纳答案by Jeff Sternal
For a low-tech solution that keeps the script in your page template, you could add this to your HTML (this could go in $(document).ready()
instead):
对于将脚本保留在页面模板中的低技术解决方案,您可以将其添加到您的 HTML(这可以$(document).ready()
改为):
<script type="text/javascript">
var missingInfo = '<%= this.MissingInfo %>' == 'True';
if (missingInfo) {
alert('Not enough information. Please try again.');
}
</script>
And add a protected property to your Page:
并向您的页面添加受保护的属性:
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;
}
回答by Darin Dimitrov
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(
this,
this.GetType(),
"popup",
"alert('hello');",
true);
}
回答by Pragnesh Patel
string script = string.Format("alert('this is alert');");
ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "redirect", script, true);
回答by Jason Berkan
While the example code in the other answers are using alert, that is just an example. Put your function in place of alert and you will have your popup.
虽然其他答案中的示例代码使用了警报,但这只是一个示例。将您的功能放在警报的位置,您将有弹出窗口。
Here is Darin's code with your function call:
这是 Darin 的函数调用代码:
protected void Button1_Click(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(
this,
this.GetType(),
"popup",
"computeCorrectValue(parameter);",
true);
}
Though I'm really not sure where you are getting the parameter that is passed into your function.
尽管我真的不确定您从何处获取传递给函数的参数。