Using RegisterDataItem on ScriptManager

Although there are different ways of sending data to client side from asp.net Ajax such as webservice, page methods, ScriptManager.RegisterStartupScript and many other ways. However my specific requirement was a bit different. I simply needed to send a value down to the client when a postback occurs through a button that is inside of an UpdatePanel, basically send additional data via ScriptManager to the client code when its sending the partial changes of the UpdatePanel down to the browser.You would want to use this technique if you are updating or want to update a control that is not part of an UpdatePanel. If the control that you are wanting to refresh is inside of an Update Panel, than simply calling UpdatePanel.Update would be sufficient enough. Here is the markup that shows how to send values down to the client code and updating a label on the client side with the data from the server.

image

 

image

 

In the above code, I am registering with the application level event called pageLoad that gets called automatically by asp.net Ajax. In the application page load event I register for pageLoad event of the script manager. I retrieve the mylabel dataitem from the script manager's dataitems collection and assign the value to update my Label on the page.

On the server side, in the button click event I am calling RegisterDataItem on the script manager control. One of the overload of the RegisterDataItem takes the control that gets effected as the first argument and second parameter is the value that you need to assign to that dataitem. Once I add my value inside the RegisterDataItem, I can easily retrieve that values from the args.Getdataitems argument property of the pageLoad event fired by ScriptManager control

No Comments