使用C#成功插入后如何显示警告框
我正在使用详细信息视图,并希望在我的代码末尾显示插入完成的警告框。 是否有一种简单的方法可以显示某种警告框,上面写着“感谢您已成功插入数据”
插入代码后,
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
Response.Write("");
在插入代码后写下这一行
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);
您可以创建一个全局方法来在Web表单应用程序中显示消息(警报)。
public 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
protected void btnSave_Click(object sender, EventArgs e) { PageUtility.MessageBox(this, "Success !"); }
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
您可以使用这种方式,但请确保没有使用Page.Redirect()
。 如果您想重定向到另一个页面,那么您可以尝试这样做:
page.aspx:
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); } }
这是代码片段背后的代码:
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"); } }
这肯定会奏效。
嘿试试这个代码。
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "Data has been saved", true);
干杯
如果您没有Page.Redirect()
,请使用此选项
Response.Write(""); //works great
但是如果你有Page.Redirect()
,请使用它
Response.Write(""); //works great
适合我。
希望这可以帮助。
您可以使用“消息”框显示成功消息。 这对我很有用。
MessageBox.Show("Data inserted successfully");