Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoke Postback from client (Javascript) 1

Status
Not open for further replies.

jubble

Programmer
Mar 6, 2002
207
GB
Is there a proper way to do a postback from Javascript while keeping your viewstate values?

I have all my Viewstates being compiled in the PreRender event in the server code but obviously this does not get called if I do a simple url call from Javascript.

If this is not possible what is the best way to pass a datatable to another form (or back to same form) via Javascript.

To go a step further is it possible to actually call a server event from Javascript? I am under the assumption that this cannot be done.
 
I dont think it's posible to send VIEW state to another form, first thing because that form would have to be exactly the same as the first form in order to get a proper VIEW state decoding.

What do you need to transfer that you couldnt send as just a simple ID,NAme etc... or Store to Session(not a good ideea to store a whole DataSet)

________
George, M
 
Hi jubble

If you have a control on your page which does nto automatically cause a postback (say a dropdownlist) and you set this to cause an autopotsback then the .NET Framework outputs the following generic javascript function to the client to cause the postback on the relevant event.
Code:
function __doPostBack(eventTarget, eventArgument) {
		var theform;
		if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
			theform = document.forms["Form1"];
		}
		else {
			theform = document.Form1;
		}
		theform.__EVENTTARGET.value = eventTarget;
		theform.__EVENTARGUMENT.value = eventArgument;
		theform.submit();
	}
In the dropdownlist onchange event the follwoing call to that function would be included to fire off the postback.

onchange="__doPostBack('ControlID','')"

You can use this to force a postback from javascript whilst also exposing the event properties of the control you wish to have initiated the postback to your code-behind. This is as close as you'll get to calling a server event from the client.

Of course if you just want to post the form back to itself and maintain viewstate, but not capture events then
Code:
document.formname.submit();
should do the trick...

Hoep this helps

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Thank you crazyboy, that was perfect.

Just for your info, shaddow, I need to transfer a datatable. Now that I can see how Javascript can do a postback while keeping ViewState there shouldn't be any reason why a table can be held in viewstate and not retrieved after postback.

jubble

 
One warning on using ViewState to store a DataTable. As ViewState data is stored within the html sent to the client if the table holds a lot of data it could seriously bloat your page's size. Depending on the connection speed of your users this may not be a problem. For example over the Intranet at work this is a technique I have used in applications succesfully as all users have the same fast connection and file size is less important to a degree. However over the public internet I would probably find a different way. It depends on the size of your table and where your app will be used.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top