繁体   English   中英

警报框未显示asp.net

[英]alert box not showing asp.net

这是我的代码

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");
            }
      }

我想要当我单击button4时,显示msgbox,之后,当我在msgbox上单击确定时,它会检查条件并响应页面。

我正在使用Visual Studio 2010,asp.net / c#

要显示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:来自ASP.NET的Alert.Show(message)代码隐藏

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

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

Response.Write(“ window.alert('” + MessageToDisplay +“');”);

在服务器端代码中,您尝试添加消息框,但是在此之后,无论哪种方法,方法都以Response.Redirect结尾-因此,您会将用户引导到新页面(即使其中一个这些网页与您来自的网页的网址相同)。

如果要让消息框显示该过程是否成功(“创建帐户”的情况),则您重定向到的任何页面都需要在呈现时包含该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")    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM