Search code examples
c#webformspostbackdatalistaddition

how to add items to datalist after postback without accessing database again


I have a DataList with a set of items (comments), users are able to leave new comments, so after they send a new comment I want to add it to the list. I know I can access de database again retrieve all data and call DataBind again or maybe cache the list of comments to avoid accessing the database, but that options are not possible, they impact on performance or memory, I Just want to access the Items property in DataList and add a new one, if it is possible with a Bind because I have a lot of properties to set, and even another DataList inside this one (replies for comments)

What's the best approach to solve this?


Solution

  • Presumably the client-side graphical control (your DataList) is bound to an object containing the list of items to be displayed. If the object was initially populated with a database call, and yet you now do not care if the database is updated, or do not care if the graphical display is out of synch with the database, and merely wish to add another item to the graphical display, you could simply add the new item to the underlying object to which the DataList has been bound, and invoke DataBind().