Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application does not shows javascript message when UPDATE PANEL is used

I am making ASP.NET application (C#). [Visual Studio 2010]

In that when i try to use client side message using javascript, it shows me message when update panel is not used.

But as soon as i added update panel to my application for preventing it from postback, Its not showing me such javascript message on insertion of record.

In the following way i am trying to display message:

Response.Write("<head><script type='text/javascript'>alert('Member Registered Sucessfully')</script></head>");

My Code:

 try
{
    con.Open();
    cmd = new SqlCommand("insert into register values(@name,@city,@address,@mobile)",con);
    cmd.Parameters.AddWithValue("@name",txtName.Text);
    cmd.Parameters.AddWithValue("@city", ddlCity.Text);
    cmd.Parameters.AddWithValue("@address",txtAddress.Text);
    cmd.Parameters.AddWithValue("@mobile", txtMobile.Text);
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    int res=cmd.ExecuteNonQuery();
    if (res > 0)
    {
        Response.Write("<head><script type='text/javascript'>alert('Member Registered Sucessfully')</script></head>");
    }
    else
    {
        Response.Write("<head><script type='text/javascript'>alert('Member Not Registered Sucessfully')</script></head>");
    }
    con.Close();

}

Please tell me where am i making mastake.

Please guide me.

like image 806
Freelancer Avatar asked Feb 17 '23 12:02

Freelancer


1 Answers

If you have update panel use ScriptManager.RegisterClientScriptBlock as below

ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Member Registered Sucessfully');", true)

You can't use Response.Write during an asynchronous postback.

like image 174
Damith Avatar answered Feb 19 '23 02:02

Damith