警告框没有显示asp.net

时间:2014-04-04 15:03:06

标签: c# javascript 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");
            }
      }

我希望当我点击按钮4,msgbox显示,之后当我在msgbox上单击确定时,它会检查条件和对页面的响应。

我正在使用visual studio 2010,asp.net / c#

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我想如果你想显示&#34;创建给用户的帐户,那么执行以下操作=

Response.Write(&#34; window.alert(&#39;&#34; + MessageToDisplay +&#34;&#39;);&#34;);

答案 2 :(得分:0)

在您的服务器端代码中,您尝试添加消息框,但在此之后,您的方法以Response.Redirect结束的任何一种方式 - 因此您将用户发送到新页面(甚至如果其中一个页面与您来自的页面是相同的URL)。

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

答案 3 :(得分:0)

因为你喜欢像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")    
相关问题