adding javascript after post back

G

Guest

I'm new to ASP .NET and have run into a problem. I have a child page open in
a web application that the user adds information to and then clicks the save
button. The information is added to a databse. If there is a problem with
adding the record to the database, I want to display the error message on the
page after post back from clicking the save button. If there are no errors I
want the child window to close after the post back.

I figured that if there were no errors I would create a javascript that
would run at page load to immediately close the window. Something similar to
adding <body onload = "window.close();">. I'm having trouble figuring out how
to add this javascript to the body element. I've added javascript before with
RegisterClientScriptBlock(), but that doesn't seem appropriate here. How
would I handle this or is there a better approach to having the window close?
 
G

Guest

Hi,

In the btnSave_Click() event, if the record is successfully saved, then do a
RegisterClientScriptBlock(), by adding "self.close()" as the javascript. I
dont find any problems in this.
 
E

Eliyahu Goldin

Add a hidden text box to the form

<input type=hidden id=inhMessage runat=server>

If the server-side detects any errors, it will write an error message in
the hidden textbox, otherwise the textbox will remain empty.

Make the body tag look like this:

<body onload="if(inhMessage.value=='')window.close();else
alert(inhMessage.value);"

Eliyahu

David Tilman said:
I'm new to ASP .NET and have run into a problem. I have a child page open in
a web application that the user adds information to and then clicks the save
button. The information is added to a databse. If there is a problem with
adding the record to the database, I want to display the error message on the
page after post back from clicking the save button. If there are no errors I
want the child window to close after the post back.

I figured that if there were no errors I would create a javascript that
would run at page load to immediately close the window. Something similar to
adding <body onload = "window.close();">. I'm having trouble figuring out how
to add this javascript to the body element. I've added javascript before with
RegisterClientScriptBlock(), but that doesn't seem appropriate here. How
would I handle this or is there a better approach to having the window
close?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top