Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Interchanging data between server and client.

Interchanging data between server and client.

Scheduled Pinned Locked Moved ASP.NET
javascriptcsharpsysadminquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    3Dizard
    wrote on last edited by
    #1

    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.

    E A 2 Replies Last reply
    0
    • 3 3Dizard

      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.

      E Offline
      E Offline
      Esmo2000
      wrote on last edited by
      #2

      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

      3 1 Reply Last reply
      0
      • E Esmo2000

        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

        3 Offline
        3 Offline
        3Dizard
        wrote on last edited by
        #3

        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 via var 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 call var 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.

        E 1 Reply Last reply
        0
        • 3 3Dizard

          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 via var 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 call var 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.

          E Offline
          E Offline
          Esmo2000
          wrote on last edited by
          #4

          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

          3 1 Reply Last reply
          0
          • E Esmo2000

            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

            3 Offline
            3 Offline
            3Dizard
            wrote on last edited by
            #5

            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?

            E 1 Reply Last reply
            0
            • 3 3Dizard

              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?

              E Offline
              E Offline
              Esmo2000
              wrote on last edited by
              #6

              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

              3 1 Reply Last reply
              0
              • 3 3Dizard

                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.

                A Offline
                A Offline
                Alvaro Mendez
                wrote on last edited by
                #7

                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 call DataBind() 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.

                3 1 Reply Last reply
                0
                • A Alvaro Mendez

                  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 call DataBind() 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.

                  3 Offline
                  3 Offline
                  3Dizard
                  wrote on last edited by
                  #8

                  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.

                  A 1 Reply Last reply
                  0
                  • E Esmo2000

                    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

                    3 Offline
                    3 Offline
                    3Dizard
                    wrote on last edited by
                    #9

                    When 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.

                    1 Reply Last reply
                    0
                    • 3 3Dizard

                      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.

                      A Offline
                      A Offline
                      Alvaro Mendez
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups