Interchanging data between server and client.
-
I have certain variables in the code-behind file and want to interchange them with the client. This is necessary because the client should show up a confirm message in javascript if one of this variables is true and then send back the information about what button the user pressed. I know I could use hidden inputs, but is this really a good method? I'm looking for something being able to transport more data, so I don't have to create a number of hidden inputs corresponding to the number of variables I want to interchange. I heard something about using ViewState. Is this a good method and how does it work (I have problems implementing it) using C# in codebehind and JS at clientside? Thanks again.
-
I have certain variables in the code-behind file and want to interchange them with the client. This is necessary because the client should show up a confirm message in javascript if one of this variables is true and then send back the information about what button the user pressed. I know I could use hidden inputs, but is this really a good method? I'm looking for something being able to transport more data, so I don't have to create a number of hidden inputs corresponding to the number of variables I want to interchange. I heard something about using ViewState. Is this a good method and how does it work (I have problems implementing it) using C# in codebehind and JS at clientside? Thanks again.
Using the viewstate is definitely a good idea if you only have to worry about the variables when you are doing your server side processing. When you use the viewstate you basically are saving variables for use with your server. If you are having to interact with the client however than you may have a slightly trickier problem on your hands. (When I say interact with the client please realise I mean on the client side... This would mean manipulation with the variables with Javascript and not after a post back). While there are other ways of doing this, its probably easiest if you just make the hidden field. In my case, I had to store whole arrays, so I used ; delimited strings and then split them by ';'. I agree though, its a tough way to have to do it. Hope that this helped, Jim
-
Using the viewstate is definitely a good idea if you only have to worry about the variables when you are doing your server side processing. When you use the viewstate you basically are saving variables for use with your server. If you are having to interact with the client however than you may have a slightly trickier problem on your hands. (When I say interact with the client please realise I mean on the client side... This would mean manipulation with the variables with Javascript and not after a post back). While there are other ways of doing this, its probably easiest if you just make the hidden field. In my case, I had to store whole arrays, so I used ; delimited strings and then split them by ';'. I agree though, its a tough way to have to do it. Hope that this helped, Jim
Yes, I in deed have to manipulate them with JS. I thought viewstate would do cause there was an example for JS in the documentation, too. So I thought i would have access to the variables, stored at serverside via
ViewState["variable"] = value;
, and get access to it in JS viavar variable = document.Form1.ViewState["variable"]
. Of course I would have to deal with the conversions from type in C# to object and back from object to type in JS. But when I callvar variable = document.Form1.ViewState["variable"]
I get an error, telling me that ViewState is no object or null. So, is this approach completely wrong or can I handle it that way and am just doing something wrong? Thanks. -
Yes, I in deed have to manipulate them with JS. I thought viewstate would do cause there was an example for JS in the documentation, too. So I thought i would have access to the variables, stored at serverside via
ViewState["variable"] = value;
, and get access to it in JS viavar variable = document.Form1.ViewState["variable"]
. Of course I would have to deal with the conversions from type in C# to object and back from object to type in JS. But when I callvar variable = document.Form1.ViewState["variable"]
I get an error, telling me that ViewState is no object or null. So, is this approach completely wrong or can I handle it that way and am just doing something wrong? Thanks.Yeah, unfortunately that isnt going to work (Actually, Im not really sure why such an example WOULD be posted... Possibly it was automatically transalated from one of the other languages? No idea). Im still new to the concept so I dont know the semantics. Viewstate works a bit like this (please dont quote me on this because I am very new to this myself). Usually you make changes on the server side, and anything that is comitted to the viewstate will be posted back to you through the web request. SO, unless Javascript is capable of taking that out of the page, reserialising the object, and actually altering the resulting post (i dont know for sure but I dont think it does at least) I think that this might not work. Again, I may be completely and utterly wrong about this. One alternative that I took was to serialise the data myself and inject it into the javascript. Then I loaded that into a variable right as Javascript loaded. Respond if you need more of an explanation, and hope this helps! Jim
-
Yeah, unfortunately that isnt going to work (Actually, Im not really sure why such an example WOULD be posted... Possibly it was automatically transalated from one of the other languages? No idea). Im still new to the concept so I dont know the semantics. Viewstate works a bit like this (please dont quote me on this because I am very new to this myself). Usually you make changes on the server side, and anything that is comitted to the viewstate will be posted back to you through the web request. SO, unless Javascript is capable of taking that out of the page, reserialising the object, and actually altering the resulting post (i dont know for sure but I dont think it does at least) I think that this might not work. Again, I may be completely and utterly wrong about this. One alternative that I took was to serialise the data myself and inject it into the javascript. Then I loaded that into a variable right as Javascript loaded. Respond if you need more of an explanation, and hope this helps! Jim
-
Thanks again. And yes, I'm very interested in your serialising method. Could you give me a hint or some code on how you did this?
Not a problem, what I did was kind of like this. Whenever I made any changes before the postback I would place it in a hidden field. We can of course do that from Javascript. Then, in my Javascript I had a function which was called something like loadVariable(). The function looked like:
function loadVariable(theValue) { // (I cant do the tab key so forgive the formatting)\ // I think that its the .value I want right? I cant remember and dont have the code handy document.getElementById('THEIDNAME').value = theValue; }
And then in my ASP I hade something like this (I am assuming that you have of course loaded the value into your ASP code already):Controls.add(new LiteralControl(""));
Whenever there is a postback the very first thing that your code will do when the page is loaded is to get Javascript to initialise the value with what was in there previously. You may have to do a bit of work to the variable into an appropriate format; I have never really tried to do it with complicated objects. They would of course have to be serialisable. Does this help? Ask if I can make it clearer, Jim -
I have certain variables in the code-behind file and want to interchange them with the client. This is necessary because the client should show up a confirm message in javascript if one of this variables is true and then send back the information about what button the user pressed. I know I could use hidden inputs, but is this really a good method? I'm looking for something being able to transport more data, so I don't have to create a number of hidden inputs corresponding to the number of variables I want to interchange. I heard something about using ViewState. Is this a good method and how does it work (I have problems implementing it) using C# in codebehind and JS at clientside? Thanks again.
3Dizard wrote: the client should show up a confirm message in javascript if one of this variables is true I've had to do this myself, but I've done it by "binding" to variables in my page class. Something like this:
function window_onload()
{
if ( <%# message != "" %> )
alert("<%# message %>");
}I have a
protected
message variable in my page class and I callDataBind()
after setting it. 3Dizard wrote: send back the information about what button the user pressed. If you make your buttons server controls (runat='server'
), you can add OnClick event handlers to them, which will automatically be called in your codebehind. This is the standard way to "know" what button the user pressed. Regards, Alvaro
Victory means exit strategy, and it's important for the President to explain to us what the exit strategy is. -- GWB, 1999.
-
3Dizard wrote: the client should show up a confirm message in javascript if one of this variables is true I've had to do this myself, but I've done it by "binding" to variables in my page class. Something like this:
function window_onload()
{
if ( <%# message != "" %> )
alert("<%# message %>");
}I have a
protected
message variable in my page class and I callDataBind()
after setting it. 3Dizard wrote: send back the information about what button the user pressed. If you make your buttons server controls (runat='server'
), you can add OnClick event handlers to them, which will automatically be called in your codebehind. This is the standard way to "know" what button the user pressed. Regards, Alvaro
Victory means exit strategy, and it's important for the President to explain to us what the exit strategy is. -- GWB, 1999.
Yeah, I already investigated on DataBind(), but with "Send back the information about what button the user pressed" I meant the button of the confirm dialog called in JavaScript. The problem now is that I want to send this information (return value of confirm() function) to the server. Any idea how to do this with your approach? Thanks for your answer.
-
Not a problem, what I did was kind of like this. Whenever I made any changes before the postback I would place it in a hidden field. We can of course do that from Javascript. Then, in my Javascript I had a function which was called something like loadVariable(). The function looked like:
function loadVariable(theValue) { // (I cant do the tab key so forgive the formatting)\ // I think that its the .value I want right? I cant remember and dont have the code handy document.getElementById('THEIDNAME').value = theValue; }
And then in my ASP I hade something like this (I am assuming that you have of course loaded the value into your ASP code already):Controls.add(new LiteralControl(""));
Whenever there is a postback the very first thing that your code will do when the page is loaded is to get Javascript to initialise the value with what was in there previously. You may have to do a bit of work to the variable into an appropriate format; I have never really tried to do it with complicated objects. They would of course have to be serialisable. Does this help? Ask if I can make it clearer, JimWhen I got you right your doing the same thing like DataBind does, but you still have to use a hidden field to send the information back to the server, right? If not I'm very interested in how this should work then. I think I'm going to use hidden input with serialising values using Control.Value property string. Thanks for your help.
-
Yeah, I already investigated on DataBind(), but with "Send back the information about what button the user pressed" I meant the button of the confirm dialog called in JavaScript. The problem now is that I want to send this information (return value of confirm() function) to the server. Any idea how to do this with your approach? Thanks for your answer.
3Dizard wrote: The problem now is that I want to send this information (return value of confirm() function) to the server. Possible alternatives: 1. Write to a hidden field and submit form. 2. Use location.href to redirect to the proper page. 3. Use Remote Scripting[^] (my article :-) ). Regards, Alvaro
Victory means exit strategy, and it's important for the President to explain to us what the exit strategy is. -- GWB, 1999.