MessageBox.Show("Here is my message");
在asp.net Web应用程序中是否有相当于此功能的东西?我可以从C#代码后台调用某些内容以向用户显示消息框吗?
此功能的示例用法:我有一个按钮,在代码后台中加载文件。当文件被加载或出现错误时,我想弹出一个消息框向用户显示结果。
对此有什么想法吗?
MessageBox.Show("Here is my message");
在asp.net Web应用程序中是否有相当于此功能的东西?我可以从C#代码后台调用某些内容以向用户显示消息框吗?
此功能的示例用法:我有一个按钮,在代码后台中加载文件。当文件被加载或出现错误时,我想弹出一个消息框向用户显示结果。
对此有什么想法吗?
你想使用一个警告框,不幸的是它不像windows forms那样好看。
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
(注:该链接为英文网页,需要自行翻译)
或者在您的解决方案中创建像这样的方法:
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>"
);
}
}
然后你可以这样使用:
MessageBox.Show("Here is my message");
public
和void
之间加上static
了。 - Heinzi我认为微软提供的这个链接是在ASP.NET中展示消息框的最佳方式。
此外,它还提供了像“是”和“否”这样的选项。
如何在你的项目中使用链接中的类的说明:
If you don't have an App_Code folder on your Project, create it.
Right click the App_Code folder and create a Class. Name it MessageBox.cs
Copy the text from the MessageBox.cs file (from the attached code) and paste it on your MessageBox.cs file.
Do the same as steps 2 & 3 for the MessageBoxCore.cs file.
Important: Right click each file MessageBox.cs and MessageBoxCore.cs and make sure the 'Build Action' is set to Compile
Add this code to your aspx page where you want to display the message box:
<asp:Literal ID="PopupBox" runat="server"></asp:Literal>
Add this code on you cs page 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 cs page. This is what will be executed when the user clicks Yes. You don't need to make another one for the NoClick
method.
[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.ascx file 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 aspx page:
<%@ Register src="~/MessageBoxUserControl.ascx" tagname="MessageBoxUserControl" tagprefix="uc1" %>
Add this line inside your aspx page (Inside your asp:Content tag if you have one)
<uc1:MessageBoxUserControl ID="MessageBoxUserControl1" runat="server" />
Save the image files 1.jpg, 2.jpg, 3.jpg, 4.jpg
from the Microsoft project above into your ~/Images/
path.
<script>
var data = '<%= JsData %>';
alert(data);
</script>
并从服务器端代码后台填充此数据
:
public partial class PageName : Page
{
protected string JsData = "your message";
\n
。alert()
,如上面的代码示例所示,或者复杂的消息框或消息横幅。有几种解决方案;如果您熟悉 CSS,这是一个非常灵活的解决方案:
创建一个外观类似于“消息框”的适当样式的 Panel
,在其中放置一个 Label
并将其 Visible
属性设置为 false
。然后,每当用户需要在 postback 后(例如按下按钮)查看消息时,从 codebehind 将 Label
的 Text
属性设置为所需的错误消息,并将 Panel
的 Visible
属性设置为 true
。
<asp:scriptmanager id="ScriptManager1" runat="server" />
{
string sMsg = "My Message";
ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "alert('" + sMsg + "')", true);
}
这是我今天刚写的一个方法,可以在页面上传递尽可能多的消息框:
/// <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);
}
如果你包含
System.Windows.forms
如果使用命名空间,可能会发生冲突。
btn_click()
{
System.Windows.Forms.MessageBox.Show("Hello");
}