Rami Far
Rami Far

Reputation: 414

response.write is not working asp.net

On button click event after inserting data I want to show message that data is inserted successfully. Here is my code

if (com2.ExecuteNonQuery() >= 1)
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!')</script>");
        }
        else
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('There is some error. Please enter again')</script>");
        }

It was working fine until I put code for empty textboxs after insertion in same event soon after above code. here is code

foreach (var control in this.Controls)
        {
            TextBox tb = control as TextBox;
            if (tb != null)
            {
                tb.Text = string.Empty;
            }
        }
        Response.Redirect("Default.aspx"); 

now textbox empty after data insertion, data is also inserting but no popup message is showing. Where is problem

Upvotes: 1

Views: 10550

Answers (2)

Xavier J
Xavier J

Reputation: 4728

I'm not 100% sure on your logic, but instead of having the server perform the redirect, do it on the client side after the call to alert:

if (com2.ExecuteNonQuery() >= 1) {
    Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!');window.location='Default.aspx';</script>");
} else {
    Response.Write("<script LANGUAGE='JavaScript' >alert('There is some error. Please enter again');window.location='Default.aspx';</script>");
}

Upvotes: 2

Jack Marchetti
Jack Marchetti

Reputation: 15754

Your Response.Redirect("Default.aspx") is run at the server level so you'll never give the client side a chance to render the javascript and therefore you won't ever see what your Response.Write is outputting.

Why don't you have an <asp:Label> control that will show the message you're looking for.

So, instead of a javascript message you could do:

tb.Text = String.Empty; //clear the textbox
label.Text = "Success Message.";  //show the message

It's been awhile since I've used WebForms but this should work if you have Viewstate enabled. You might also want to look into UpdatePanels for this.

Upvotes: 1

Related Questions