如何在ASP.NET中显示消息框?

Fre*_*cer 13 javascript c# asp.net

我想在成功保存任何项目时显示一个消息框.我用谷歌搜索并尝试了不同的解决方案,但它们都没有奏效.这是我正在使用的代码:

try
{
    con.Open();
    string pass="abc";
    cmd = new SqlCommand("insert into register values('" + 
                                       txtName.Text + "','" + 
                                       txtEmail.Text + "','" + 
                                       txtPhoneNumber.Text + "','" + 
                                       ddlUserType.SelectedText + "','" + 
                                       pass + "')", con);

    cmd.ExecuteNonQuery();
    con.Close();
    Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");
}
catch (Exception ex)
{

}
finally
{
    con.Close();
}
Run Code Online (Sandbox Code Playgroud)

(我正在使用Firefox,如果这很重要)

Hit*_*iya 25

@freelancer如果您使用的是ScriptManager,请尝试使用此代码进行消息.

string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(), 
                      "ServerControlScript", script, true);
Run Code Online (Sandbox Code Playgroud)


小智 15

在页面中创建MsgBox的方法.


public void MsgBox(String ex, Page pg,Object obj) 
{
    string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
    Type cstype = obj.GetType();
    ClientScriptManager cs = pg.ClientScript;
    cs.RegisterClientScriptBlock(cstype, s, s.ToString());
}
Run Code Online (Sandbox Code Playgroud)

当你想使用msgbox时就把这一行

MsgBox("! your message !", this.Page, this);
Run Code Online (Sandbox Code Playgroud)


Md.*_*mud 6

试试这个,它在我的浏览器中工作正常:

你的回复写代码应该是

Response.Write("<script>alert('login successful');</script>");
Run Code Online (Sandbox Code Playgroud)

希望这有效