Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a messagebox in ASP.NET?

I want to show a message box on the successful save of any item. I googled it and tried different solutions, but none of them worked. Here is the code I am using:

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

(I am using Firefox, if that matters)

like image 786
Freelancer Avatar asked Mar 04 '13 07:03

Freelancer


3 Answers

@freelancer If you are using ScriptManager then try this code for message..

string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(), 
                      "ServerControlScript", script, true);
like image 198
Hitesh Bavaliya Avatar answered Sep 22 '22 15:09

Hitesh Bavaliya


Make a method of MsgBox in your page.


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

and when you want to use msgbox just put this line

MsgBox("! your message !", this.Page, this);
like image 37
Harsh Varudkar Avatar answered Sep 19 '22 15:09

Harsh Varudkar


just try this, it works fine in my browser:

your response writing code should be

Response.Write("<script>alert('login successful');</script>");

Hope this works

like image 40
Md. Arafat Al Mahmud Avatar answered Sep 22 '22 15:09

Md. Arafat Al Mahmud