如何在asp.net中显示消息框

我正在使用C#创建一个网站,我正在尝试显示一个消息框。 我正在尝试使用JavaScript来处理这种情况,如果我执行以下操作它会运行:

Response.Write("alert('Login Successful')"); 

但是,如果相反,我这样做:

 Response.Write("alert('Login Successful')"); Response.Redirect("~/admin.aspx"); 

消息框未显示。

为什么这样,我该如何解决?

通过在实际向客户端发送302重定向后立即执行Response.Redirect,因此警报实际上永远不会在用户的浏览器中呈现。 相反,尝试这样的事情

  Response.Write(""); 

在向用户显示alert之前,您的Response.Redirect调用将触发并重定向浏览器。

由于以下行,写入响应的JavaScript未运行:

 Response.Redirect("~/admin.aspx"); 

这会将响应从当前页面重定向到Admin.aspx。 写入响应的任何其他内容将不会被呈现和执行,因为正在指示浏览器导航到新位置。

使用

 ClientScript.RegisterStartupScript(Me.GetType(), "fnCall", "") Response.Redirect("~/admin.aspx"); 

希望有所帮助

另外,要从UpdatePanel注册脚本,您应该使用:

 ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true); 
 ""; 

编辑:

通常,我们通过编写一个带有message作为参数的公共方法来在asp.net中显示一个消息框,如下所示。

 public void UserMsgBox(string sMsg) { try { StringBuilder sb = new StringBuilder(); System.Web.UI.Control oFormObject = null; sMsg = sMsg.Replace("'", "\\'"); sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34)); sMsg = sMsg.Replace(Constants.vbCrLf, "\\n"); sMsg = ""; sb = new StringBuilder(); sb.Append(sMsg); foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) { oFormObject = oFormObject_loopVariable; if (oFormObject is HtmlForm) { break; // TODO: might not be correct. Was : Exit For } } oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString())); } catch (Exception ex) { } } 

我也用这种方式,但代码项目以更好的方式提供

  protected void Button1_Click(object sender, EventArgs e) { ClientScriptManager CSM = Page.ClientScript; if (!ReturnValue()) { string strconfirm = ""; CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false); } } bool ReturnValue() { return false; } 

这是迟到的,但这是正确的答案,因为下面没有你已经检查过的答案,你不能使用响应重定向,因为它会重定向页面,然后你可以显示在页面末尾附加的消息框。 你应该使用窗口定位方法:

 Response.Write(""); 

如果您使用的是更新面板,则可以在.cs页面中使用此代码来显示消息框

 ScriptManager.RegisterStartupScript(this, this.GetType(), "myalert", "alert('Type here your message...')", true); 

要么

 ScriptManager.RegisterStartupScript(this.ControlID, this.GetType(), "myalert", "alert('Type here your message')", true); 

或者如果您没有使用更新面板,此代码将显示消息框

 ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Type here your message')", true); 

这是一个示例程序,您可以在其中将自己的消息调用到消息框中。它非常适合您。 你必须尝试我的榜样

 protected void btnSubmit_Click(object sender, EventArgs e) { try { if (RadioButtonList1.SelectedItem.ToString() == "Sum Of Digit") { string a = tbInput.Text; int sum = 0; for (int i = 0; i < a.Length; i++) { sum = sum + Convert.ToInt32(a[i].ToString()); } lblResult.Text = sum.ToString(); } else if (RadioButtonList1.SelectedItem.ToString() == "InterChange Number") { string interchange = tbInput.Text; string result = ""; int condition = Convert.ToInt32(interchange.ToString()); if (condition <= 99) { result = interchange[interchange.Length - 1].ToString() + interchange[0].ToString(); lblResult.Text = result.ToString(); } else { MyMessageBox("Number Must Be Less Than 99"); } } else if (RadioButtonList1.SelectedItem.ToString() == "Sum Of First n last Digit") { //example } else { MyMessageBox("Not Found"); } } catch (Exception ex) { MyMessageBox(ex.ToString()); } } public void MyMessageBox(string text) { string script = "alert('"+text+"');"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScripts", script, true); } }