C# ASP.NET Web 应用程序消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9720143/
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
ASP.NET Web Application Message Box
提问by Baxter
In an asp.net windows forms application, in the C# code behind you can use:
在 asp.net windows 窗体应用程序中,您可以在后面的 C# 代码中使用:
MessageBox.Show("Here is my message");
Is there any equivalent in a asp.net web application? Can I call something from the C# code behind that will display a message box to the user?
asp.net web 应用程序中是否有任何等价物?我可以从后面的 C# 代码中调用一些东西来向用户显示一个消息框吗?
Example usage of this: I have a button that loads a file in the code behind. When the file is loaded or if there is an error I would like to popup a message to the user stating the result.
示例用法:我有一个按钮,可以在后面的代码中加载文件。当文件加载或出现错误时,我想向用户弹出一条消息,说明结果。
Any ideas on this?
对此有何想法?
采纳答案by Gage
You want to use an Alert. Unfortunately it's not as nice as with windows forms.
您想使用警报。不幸的是,它不如 Windows 窗体好。
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
Similar to this question here: http://forums.asp.net/t/1461308.aspx/1
类似于这里的这个问题:http: //forums.asp.net/t/1461308.aspx/1
回答by ek_ny
not really. Server side code is happening on the server--- you can use javascript to display something to the user on the client side, but it obviously will only execute on the client side. This is the nature of a client server web technology. You're basically disconnected from the server when you get your response.
并不真地。服务器端代码发生在服务器上——您可以使用 javascript 在客户端向用户显示某些内容,但它显然只会在客户端执行。这是客户端服务器网络技术的本质。当您收到响应时,您基本上与服务器断开了连接。
回答by Yahia
回答by Shree
Why should not use jquery popup for this purpose.I use bpopupfor this purpose.See more about this.
http://dinbror.dk/bpopup/
为什么不应该为此目的使用 jquery 弹出窗口。我为此目的使用bpopup。查看更多相关信息。
http://dinbror.dk/bpopup/
回答by Saeb Amini
There are a few solutions; if you are comfortable with CSS, here's a very flexible solution:
有几个解决方案;如果您对 CSS 感到满意,这里有一个非常灵活的解决方案:
Create an appropriately styled Panelthat resembles a "Message Box", put a Labelin it and set its Visibleproperty to false. Then whenever the user needs to see a message aftera postback (e.g. pushing a button), from codebehind set the Labels Textproperty to the desired error message and set the Panel's Visibleproperty to true.
创建一个Panel类似“消息框”的适当样式,将 aLabel放入其中并将其Visible属性设置为false。然后每当用户需要在回发(例如按下按钮)后查看消息时,从代码隐藏将LabelsText属性设置为所需的错误消息并将Panel'sVisible属性设置为true。
回答by anu
Right click the solution explorer and choose the add reference.onedialog box will be appear. On that select (.net)-> System.windows.form. Imports System.Windows.Forms (vb)and using System.windows.forms(C#)copy this in your coding and then write messagebox.show("").
右键单击解决方案资源管理器并选择reference.one将出现添加对话框。在那个选择(.net)-> System.windows.form。在您的编码中导入System.Windows.Forms (vb)并使用System.windows.forms(C#)复制此内容,然后编写messagebox.show("").
回答by arakami
Just add the namespace:
只需添加命名空间:
System.Windows.forms
to your web application reference or what ever, and you have the access to your:
到您的 Web 应用程序参考或其他任何内容,您可以访问您的:
MessageBox.Show("Here is my message");
I tried it and it worked.
我试过了,它奏效了。
Good luck.
祝你好运。
回答by user2678089
if you will include
如果你将包括
System.Windows.forms
as namespace then it will conflict . Use
作为命名空间,那么它将发生冲突。用
btn_click()
{
System.Windows.Forms.MessageBox.Show("Hello");
}
回答by Ali Humayun
Or create a method like this in your solution:
或者在您的解决方案中创建一个这样的方法:
public static class MessageBox {
public static void Show(this Page Page, String Message) {
Page.ClientScript.RegisterStartupScript(
Page.GetType(),
"MessageBox",
"<script language='javascript'>alert('" + Message + "');</script>"
);
}
}
Then you can use it like:
然后你可以像这样使用它:
MessageBox.Show("Here is my message");
回答by Pabinator
Just for the records.
只是为了记录。
Here is a link from Microsoftthat I think is the best way to present a MessageBoxin ASP.Net
这是来自 Microsoft 的链接,我认为这是在ASP.Net 中呈现MessageBox的最佳方式
Also it presents choices like Yesand NO.
它还提供了Yes和NO等选项。
Instructions on how to get the class from the linkworking on your project:
有关如何从处理您的项目的链接获取课程的说明:
- If you don't have an App_Codefolder on your Project, create it.
- Right click the App_Codefolder and create a Class. Name it MessageBox.cs
- Copy the text from the MessageBox.csfile (from the attached code) and paste it on your MessageBox.csfile.
- Do the same as steps 2 & 3 for the MessageBoxCore.csfile.
- Important:Right click each file MessageBox.csand MessageBoxCore.csand make sure the 'Build Action'is set to Compile
Add this code to your aspxpage where you want to display the message box:
<asp:Literal ID="PopupBox" runat="server"></asp:Literal>Add this code on you cspage where you want to decision to be made:
string title = "My box title goes here"; string text = "Do you want to Update this record?"; MessageBox messageBox = new MessageBox(text, title, MessageBox.MessageBoxIcons.Question, MessageBox.MessageBoxButtons.YesOrNo, MessageBox.MessageBoxStyle.StyleA); messageBox.SuccessEvent.Add("YesModClick"); PopupBox.Text = messageBox.Show(this);Add this method to your cspage. This is what will be executed when the user clicks Yes. You don't need to make another one for the
NoClickmethod.[WebMethod] public static string YesModClick(object sender, EventArgs e) { string strToRtn = ""; // The code that you want to execute when the user clicked yes goes here return strToRtn; }Add a WebUserControl1.ascxfile to your root path and add this code to the file:
<link href="~/Styles/MessageBox.css" rel="stylesheet" type="text/css" /> <div id="result"></div> <asp:ScriptManager runat="server" ID="scriptManager" EnablePageMethods="True"> </asp:ScriptManager> //<-- Make sure you only have one ScriptManager on your aspx page. Remove the one on your aspx page if you already have one.Add this line on top of your aspxpage:
<%@ Register src="~/MessageBoxUserControl.ascx" tagname="MessageBoxUserControl" tagprefix="uc1" %>Add this line inside your aspxpage (Inside your asp:Contenttag if you have one)
<uc1:MessageBoxUserControl ID="MessageBoxUserControl1" runat="server" />Save the image files
1.jpg, 2.jpg, 3.jpg, 4.jpgfrom the Microsoft project above into your~/Images/path.Done
- 如果您的项目中没有App_Code文件夹,请创建它。
- 右键单击App_Code文件夹并创建一个类。将其命名为MessageBox.cs
- 从MessageBox.cs文件(来自附加代码)复制文本并将其粘贴到MessageBox.cs文件中。
- 对MessageBoxCore.cs文件执行与步骤 2 和 3 相同的操作。
- 重要提示:右键单击每个文件MessageBox.cs和MessageBoxCore.cs并确保“构建操作”设置为“编译”
将此代码添加到要显示消息框的aspx页面:
<asp:Literal ID="PopupBox" runat="server"></asp:Literal>在您要做出决定的cs页面上添加此代码:
string title = "My box title goes here"; string text = "Do you want to Update this record?"; MessageBox messageBox = new MessageBox(text, title, MessageBox.MessageBoxIcons.Question, MessageBox.MessageBoxButtons.YesOrNo, MessageBox.MessageBoxStyle.StyleA); messageBox.SuccessEvent.Add("YesModClick"); PopupBox.Text = messageBox.Show(this);将此方法添加到您的cs页面。这是当用户单击 Yes 时将执行的操作。您不需要为该
NoClick方法制作另一个。[WebMethod] public static string YesModClick(object sender, EventArgs e) { string strToRtn = ""; // The code that you want to execute when the user clicked yes goes here return strToRtn; }将WebUserControl1.ascx文件添加到您的根路径并将此代码添加到文件中:
<link href="~/Styles/MessageBox.css" rel="stylesheet" type="text/css" /> <div id="result"></div> <asp:ScriptManager runat="server" ID="scriptManager" EnablePageMethods="True"> </asp:ScriptManager> //<-- Make sure you only have one ScriptManager on your aspx page. Remove the one on your aspx page if you already have one.在您的aspx页面顶部添加此行:
<%@ Register src="~/MessageBoxUserControl.ascx" tagname="MessageBoxUserControl" tagprefix="uc1" %>在你的aspx页面中添加这一行(在你的asp:Content标签中,如果你有的话)
<uc1:MessageBoxUserControl ID="MessageBoxUserControl1" runat="server" />将上述
1.jpg, 2.jpg, 3.jpg, 4.jpgMicrosoft 项目中的图像文件保存到您的~/Images/路径中。完毕
Hope it helps.
希望能帮助到你。
Pablo
巴勃罗

