简体   繁体   English

ASP.NET Web 应用程序消息框

[英]ASP.NET Web Application Message Box

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? asp.net web 应用程序中是否有任何等价物? Can I call something from the C# code behind that will display a message box to the user?我可以从后面的 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?对此有何想法?

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

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

Here is a link from Microsoft that I think is the best way to present a MessageBox in ASP.NET 这是来自 Microsoft 的链接,我认为这是在 ASP.NET 中呈现 MessageBox 的最佳方式

Also it presents choices like Yes and No.它还提供了 Yes 和 No 之类的选择。

Instructions on how to get the class from the link working on your project:有关如何从处理您的项目的链接获取课程的说明:

  1. If you don't have an App_Code folder on your Project, create it.如果您的项目中没有App_Code文件夹,请创建它。

  2. Right click the App_Code folder and create a Class.右键单击App_Code文件夹并创建一个类。 Name it MessageBox.cs将其命名为MessageBox.cs

  3. Copy the text from the MessageBox.cs file ( from the attached code ) and paste it on your MessageBox.cs file.MessageBox.cs文件( 来自附加代码)复制文本并将其粘贴到MessageBox.cs文件中。

  4. Do the same as steps 2 & 3 for the MessageBoxCore.cs file.MessageBoxCore.cs文件执行与步骤 2 和 3 相同的操作。

  5. Important: Right click each file MessageBox.cs and MessageBoxCore.cs and make sure the 'Build Action' is set to Compile重要提示:右键单击每个文件MessageBox.csMessageBoxCore.cs并确保“构建操作”设置为“编译”

  6. Add this code to your aspx page where you want to display the message box:将此代码添加到要显示消息框的aspx页面:

     <asp:Literal ID="PopupBox" runat="server"></asp:Literal>
  7. Add this code on you cs page where you want to decision to be made:在您要做出决定的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);
  8. Add this method to your cs page.将此方法添加到您的cs页面。 This is what will be executed when the user clicks Yes.这是当用户单击 Yes 时将执行的操作。 You don't need to make another one for the NoClick method.您不需要为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; }
  9. Add a WebUserControl1.ascx file to your root path and add this code to the file: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.
  10. Add this line on top of your aspx page:在您的aspx页面顶部添加此行:

     <%@ Register src="~/MessageBoxUserControl.ascx" tagname="MessageBoxUserControl" tagprefix="uc1" %>
  11. Add this line inside your aspx page (Inside your asp:Content tag if you have one)在你的aspx页面中添加这一行(在你的asp:Content标签中,如果你有的话)

     <uc1:MessageBoxUserControl ID="MessageBoxUserControl1" runat="server" />
  12. Save the image files 1.jpg, 2.jpg, 3.jpg, 4.jpg from the Microsoft project above into your ~/Images/ path.将上面 Microsoft 项目中的图像文件1.jpg, 2.jpg, 3.jpg, 4.jpg保存到您的~/Images/路径中。

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.服务器端代码发生在服务器上——您可以使用 javascript 在客户端向用户显示某些内容,但它显然只会在客户端执行。 This is the nature of a client server web technology.这是客户端服务器网络技术的本质。 You're basically disconnected from the server when you get your response.当您收到响应时,您基本上与服务器断开了连接。

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? asp.net Web应用程序中是否有任何等效项? Can I call something from the C# code behind that will display a message box to the user?我可以从后面的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?有什么想法吗?

Why should not use jquery popup for this purpose.I use bpopup for this purpose.See more about this.为什么不应该为此目的使用 jquery popup。我为此目的使用bpopup 。查看更多相关信息。
http://dinbror.dk/bpopup/ http://dinbror.dk/bpopup/

'ASP.net MessageBox 'ASP.net 消息框

'Add a scriptmanager to the ASP.Net Page '向 ASP.Net 页面添加脚本管理器

<asp:scriptmanager id="ScriptManager1" runat="server" />

try:尝试:

{

  string sMsg = "My Message";

  ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "alert('" + sMsg + "')", true);

}

As others already pointed out, a message box will be clientside Javascript.正如其他人已经指出的那样,消息框将是客户端 Javascript。 So the problem then is how to force a clientside JS message box from the server side.所以问题是如何从服务器端强制客户端 JS 消息框。 A simple solution is to include this in the HTML:一个简单的解决方案是将其包含在 HTML 中:

<script>
    var data = '<%= JsData %>';
    alert(data);
</script>

and to fill this data from the server side code-behind:并从服务器端代码隐藏填充此data

public partial class PageName : Page
{
    protected string JsData = "your message";

Note that the string value should be a Javascript string, ie be a one-liner, but it may contain escaped newlines as \\n .请注意,字符串值应该是 Javascript 字符串,即单行,但它可能包含转义的换行符\\n

Now you can use all your Javascript or JQuery skills and tricks to do whatever you want with that message text on the clientside, such as display a simple alert() , as shown in the above code sample, or sophisticated message box or message banner.现在,您可以使用所有 Javascript 或 JQuery 技能和技巧在客户端对消息文本执行任何您想要的操作,例如显示一个简单的alert() ,如上面的代码示例所示,或者复杂的消息框或消息横幅。

(Note that popups are sometimes frowned upon and blocked) (请注意,弹出窗口有时会被拒绝并被阻止)

Note also that, due to the HTTP protocol, the message can only be shown in response to an HTTP request that the user sends to the server.另请注意,由于 HTTP 协议,该消息只能在响应用户发送到服务器的 HTTP 请求时显示。 Unlike WinForm apps, the web server cannot push a message to the client whenever it sees fit.与 WinForm 应用程序不同,Web 服务器无法在它认为合适时将消息推送到客户端。

If you want to show the message only once, and not after the user refreshes the page with F5, you could set and read a cookie with javascript code.如果您只想显示一次消息,而不是在用户使用 F5 刷新页面之后,您可以使用 javascript 代码设置和读取 cookie。 In any case, the nice point with this method is that it is an easy way to get data from the server to the javascript on the client, and that you can use all javascript features to accomplish anything you like.在任何情况下,此方法的好处在于它是一种从服务器获取数据到客户端上的 javascript 的简单方法,并且您可以使用所有 javascript 功能来完成您喜欢的任何事情。

Here's a method that I just wrote today, so that I can pass as many message boxes to the page as I want to:这是我今天刚写的一个方法,这样我就可以将尽可能多的消息框传递给页面:

/// <summary>
/// Shows a basic MessageBox on the passed in page
/// </summary>
/// <param name="page">The Page object to show the message on</param>
/// <param name="message">The message to show</param>
/// <returns></returns>
public static ShowMessageBox(Page page, string message)
{
    Type cstype = page.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = page.ClientScript;

    // Find the first unregistered script number
    int ScriptNumber = 0;
    bool ScriptRegistered = false;
    do
    {
        ScriptNumber++;
        ScriptRegistered = cs.IsStartupScriptRegistered(cstype, "PopupScript" + ScriptNumber);
    } while (ScriptRegistered == true);

    //Execute the new script number that we found
    cs.RegisterStartupScript(cstype, "PopupScript" + ScriptNumber, "alert('" + message + "');", true);
}

There are a few solutions;有几个解决方案; if you are comfortable with CSS, here's a very flexible solution:如果您对 CSS 感到满意,这里有一个非常灵活的解决方案:

Create an appropriately styled Panel that resembles a "Message Box", put a Label in it and set its Visible property to false .创建一个类似“消息框”的适当样式的Panel ,在其中放置一个Label并将其Visible属性设置为false Then whenever the user needs to see a message after a postback (eg pushing a button), from codebehind set the Label s Text property to the desired error message and set the Panel 's Visible property to true .然后,每当用户回发(例如按下按钮)需要查看消息时,从代码隐藏将LabelText属性设置为所需的错误消息,并将PanelVisible属性设置为true

if you will include如果你将包括

System.Windows.forms

as namespace then it will conflict .作为命名空间,那么它将发生冲突。 Use采用

btn_click()
{

System.Windows.Forms.MessageBox.Show("Hello");

}

You need to reference the namespace您需要引用命名空间


using System.Windows.Form;

and then add in the code然后加入代码

protected void Button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(" Hi....");

    }

Right click the solution explorer and choose the add reference.one dialog box will be appear.右键单击解决方案资源管理器并选择添加reference.one 。将出现一个对话框。 On that select (.net)-> System.windows.form .在那个选择(.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("") .导入System.Windows.Forms (vb)并使用System.windows.forms(C#)将其复制到您的编码中,然后编写messagebox.show("")

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.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM