警报框未显示 asp.net

本文关键字:asp net 显示 | 更新日期: 2023-09-27 17:56:55

这是我的代码

ASPX 代码:

<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Insert" />

C#:

public void MsgBox(String MessageToDisplay)
    {
        Label lblForMsg = new Label();
        lblForMsg.Text = "<script language='javascript'>window.alert('" + MessageToDisplay + "')</script>";
        Page.Controls.Add(lblForMsg);
    }
protected void Button4_Click(object sender, EventArgs e)
    {
SqlCommand com = new SqlCommand("insert into employe values(@id,@pass)", con);
            SqlParameter obj1 = new SqlParameter("@Id", DbType.StringFixedLength);
            obj1.Value = TextBox4.Text;
            com.Parameters.Add(obj1);
            SqlParameter obj2 = new SqlParameter("@pass", DbType.StringFixedLength);
            obj2.Value = TextBox5.Text;
            com.Parameters.Add(obj2);    
            com.ExecuteNonQuery();
            con.Close();
            MsgBox("Account Created");
            if (Session["regis"] == null)
            {
                Response.Redirect("Profile.aspx");
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
      }
我希望当我单击按钮4时,msgbox

显示,然后当我在msgbox上单击"确定"时,它会检查条件和对页面的响应。

我正在使用Visual Studio 2010,ASP.NET/C#

警报框未显示 asp.net

要显示 javascript 警报消息,您应该使用 RegisterClientScriptBlock

public static void ShowMessageAndRedirect(string message, string lpRedirectPage) 
{                
   string cleanMessage = MessageToDisplay.Replace("'", "''");                               
   Page page = HttpContext.Current.CurrentHandler as Page; 
   string script = string.Format("alert('{0}');", cleanMessage);
   script += " window.location.href='" + lpRedirectPage+ "';"
   if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) 
   {
       page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */);
   } 
} 

类似的问题在这里:JavaScript:Alert.Show(message) from ASP.NET Code-Behind

 if (Session["regis"] == null)
 {
     ShowMessageAndRedirect("Account Created","Profile.aspx");
 }
 else
 {
    ShowMessageAndRedirect("Account Created","Login.aspx");
  }

我认为如果您想显示"为用户创建的帐户",请执行以下操作=

Response.Write("window.alert('" + MessageToDisplay + "');");

在服务器端代码中,您尝试添加消息框,但紧接着,无论哪种方式,您的方法都以 Response.Redirect 结尾 - 因此您将用户发送到新页面(即使其中一个页面与您来自的页面的 URL 相同)。

如果您希望消息框显示该过程是否成功(您的"帐户创建"方案),那么无论重定向到哪个页面,都需要在呈现时包含该 javascript 消息框。

因为你喜欢像使用 Windows 表单一样使用 MsgBox.show()。本课程将帮助您(vb.net)

Imports Microsoft.VisualBasic
Public Class MsgBox
Public Shared Sub Show(ByRef page As Page, ByVal strMsg As String)
    Try
        Dim lbl As New Label
        lbl.Text = "<script language='javascript'>" & Environment.NewLine _
                    & "window.alert(" & "'" & strMsg & "'" & ")</script>"
        page.Controls.Add(lbl)
    Catch ex As Exception
    End Try
End Sub 
End Class

从 aspx.page 内部可以这样称呼

 Msgbox.show(Me,"Alert something")