3

I have an update panel with a timer control set up to automatically check for some data updates every minute or so.

If it sees that the data updates, it is set to call a local script with the serialized JSON data.

ScriptManager.RegisterStartupScript(UpdateField, GetType(HiddenField), ACTION_CheckHistoryVersion, "updateData(" & data & ");", True)

where "data" might look something like

{
   "someProperty":"foo",
   "someOtherProperty":"bar",
   "someList":[
     {"prop1":"value"},
     {"prop2":"value"}, ...
   ],
   "someOtherList":[{},...,{}]
}

"data" can get quite large, and sometimes only a few items change.

The problem I am having is this. Every time I send this back to the client, it gets added as a brand new script block and the existing blocks do not get removed or replaced.

output looks something like this:

<script type="text/javascript">
  updateData({
       "someProperty":"foo",
       "someOtherProperty":"bar",
       "someList":[
         {"prop1":"value"},
         {"prop2":"value"}, ...
       ],
       "someOtherList":[{},...,{}]
    });
</script>
<script type="text/javascript">
  updateData({
       "someProperty":"foo",
       "someOtherProperty":"bar",
       "someList":[
         {"prop1":"changed"},
         {"prop2":"val"}, ...
       ],
       "someOtherList":[{},...,{}]
    });
</script>
<script type="text/javascript">
  updateData({
       "someProperty":"foos",
       "someOtherProperty":"ball",
       "someList":[
         {"prop1":"changed"},
         {"prop2":"val"}, ...
       ]
    });
</script>

with a new script block being created every time there is a change in the data.

Over time the amount of data accumulating on the browser could get potentially huge if we just keep adding this and I can't imagine how most people's browser would take it, but I don't think it could be good.

Does anyone know if there is a way to just replace the code that has been sent back to the browser rather than continuously adding it like this?

4
  • I found a hack and I've edited my question to show it. Commented Sep 11, 2012 at 21:59
  • 1
    Can you simply use webservice? Commented Sep 11, 2012 at 22:14
  • Please consider posting your hack as an answer rather than an update to your question, as that is proper protocol here. You're even encouraged to accept your own answer if nothing better comes along during the delay. Commented Sep 11, 2012 at 22:15
  • Okay, removed fix from question and added as an answer. When I added it to the question, I was under the assumption that I could not answer my own question the same day. That used to be how things worked around here. Perhaps that changed and I was unaware. Commented Sep 12, 2012 at 14:32

2 Answers 2

2

I came up with a hack that seems to work in my situation.

I am using jQuery to find the script tag that I am creating and remove it after it has been called.

Here is an example:

First I generate a guid:

Dim guidText as string = GUID.NewGuid().ToString()

I create a function like the following:

function RemoveThisScript(guid){

   $("script").each(function(){
     var _this = $(this);
     if(_this.html().indexOf(guid)>-1)
        _this.remove();
   });

}

Then I add the following code to my output string:

... & " RemoveThisScript('" & guidText & "');"

This causes jQuery to look through all the scripts on the page for one that has the GUID (essentially the one calling the function) and removes it from the DOM.

Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend to use web service with some webmethod which you will call inside window.setInterval. In success handler of your webmethod (on client side) you can just take response and do whatever you want with it. And it will not be saved in your page (well, if you will do everything wrong). Benefit is that you will minimize request size(updatepanel will pass all your viewstate data, which could be large enough) and will limit server resources usage (update panel is causing full page live cycle, suppose slightly modified, but anyway - all those page_load, page_init, etc...) and with web service you will only what you need.

Here is an article where you can see how it could be created and used on client side. Looks like good enough.

1 Comment

So, this was an option, indeed, I have already looked at encosia.com/… , but it would require a lot more work. On the code behind for the page, our organization has a number of objects that utilize the page context for processing and while I can do a bunch of work to hook these up manually to the page context, my preference was not to have to do this, as it is not trivial. Since this answer will be the correct one for most people, I will accept it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.