如何在 ASP.NET 中显示来自 C# 的警报框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16370465/
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 display an alert box from C# in ASP.NET?
提问by moe
I am using a detail-viewand would like to display an alert-box at the end of my code block that says:
我正在使用 adetail-view并希望在我的代码块末尾显示一个警告框,上面写着:
Thank you! Your data has been inserted successfully.
谢谢!您的数据已成功插入。
Is there a simple way to do this from the C#code behind of my ASP.NETweb pages?
有没有一种简单的方法可以从C#我的ASP.NET网页背后的代码中做到这一点?
采纳答案by Mudassir Hasan
After insertion code,
插入代码后,
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
回答by Amit Singh
Write this line after your insert code
在插入代码后写下这一行
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);
回答by Neeraj Dubey
Hey Try This Code.
嘿试试这个代码。
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "Data has been saved", true);
Cheers
干杯
回答by Mayank
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
You can use this way, but be sure that there is no Page.Redirect()is used.
If you want to redirect to another page then you can try this:
您可以使用这种方式,但请确保没有Page.Redirect()使用。如果你想重定向到另一个页面,那么你可以试试这个:
page.aspx:
页面.aspx:
<asp:Button AccessKey="S" ID="submitBtn" runat="server" OnClick="Submit" Text="Submit"
Width="90px" ValidationGroup="vg" CausesValidation="true" OnClientClick = "Confirm()" />
JavaScript code:
JavaScript 代码:
function Confirm()
{
if (Page_ClientValidate())
{
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Data has been Added. Do you wish to Continue ?"))
{
confirm_value.value = "Yes";
}
else
{
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
}
and this is your code behind snippet :
这是您的代码片段背后的代码:
protected void Submit(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
Response.Redirect("~/AddData.aspx");
}
else
{
Response.Redirect("~/ViewData.aspx");
}
}
This will sure work.
这肯定会奏效。
回答by darshil.chapadia
Response.Write("<script>alert('Data inserted successfully')</script>");
回答by karthikeyan_somu
You can use Message box to show success message. This works great for me.
您可以使用消息框来显示成功消息。这对我很有用。
MessageBox.Show("Data inserted successfully");
MessageBox.Show("Data inserted successfully");
回答by sebu
You can create a global method to show message(alert) in your web form application.
您可以创建一个全局方法来在您的 Web 表单应用程序中显示消息(警报)。
public static class PageUtility
{
public static void MessageBox(System.Web.UI.Page page,string strMsg)
{
//+ character added after strMsg "')"
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alertMessage", "alert('" + strMsg + "')", true);
}
}
webform.aspx
网页表单.aspx
protected void btnSave_Click(object sender, EventArgs e)
{
PageUtility.MessageBox(this, "Success !");
}
回答by Ahmad Tijani
If you don't have a Page.Redirect(), use this
如果你没有Page.Redirect(),请使用这个
Response.Write("<script>alert('Inserted successfully!')</script>"); //works great
But if you do have Page.Redirect(), use this
但如果你有Page.Redirect(),使用这个
Response.Write("<script>alert('Inserted..');window.location = 'newpage.aspx';</script>"); //works great
works for me.
为我工作。
Hope this helps.
希望这可以帮助。

