RegisterDataItem is a subtle yet powerful member of the ScriptManager class. Unknown to many developers of ASP.Net AJAX is the fact that there can be a back-end data stream flowing from the server to the controls on the client whenever a partial rendering event takes place anywhere on the page. Through RegisterDataItem, you get to register a particular block of data (raw data or JSON serialized data) to a particular control on the client; and client code you place on the page can very easily update that control with its registered data. Example...
Say you have a calendar control on your web form inside an UpdatePanel, and outside the UpdatePanel there exists a Label control whose text should display the selected date of the calendar:
This design is for purposes of this example only; you probably wouldn't always set your system up this way, but for now it suffices.
The question is, how would you go about updating the Label as a result of a refresh of UpdatePanel1 in the example above? The answer is the RegisterDataItem.
In the postback method for clicking a particular date in the calendar, do this:
protected void Calendar1_SelectionChanged(object sender, EventArgs e) { this.ScriptManager1.RegisterDataItem(this.Label_SelectedDate, this.Calendar1.SelectedDate.ToString()); }
The result will be, whenever you select a date within the calendar, the AJAX runtime will properly update the contents of the label:
This works because the ASP.net AJAX runtime sends the serialized data down to the client framework during the partial rendering of the calendar; the client framework will snag the appropriate bits from this feed and update the controls accordingly through the script we wrote. The "trick" is registering a method with the pageLoading event of the PageRequestManager.
Remember Me
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.