Datalist Dynamic Controls - ItemDataBound

JamesAStewart used Ask the Experts™
I am trying to write a messaging system which will allow people to communicate and write messages to each other.

I have a datalist which is populated by an ObjectDataSource. On the ItemDataBound event of the datalist I pick out the data which I want to display and show it in a selection of labels and textboxes .etc... This part of my system works okay.

What I am now trying to do is as follows. We want to allow users to reply to messages and then display them under the message they have replied to. Again, this part of the system works ok, but i now want to add buttons into the replies to allow users to delete .etc... The replies are populated dynamically on the ItemDataBound event, so that we can check whether there are any replies to the each of the messages in the datalist, it has to be done on this event.

After I have dynamically added the buttons in the ItemDataBound event they display on screen fine but when i click the button all of the replies dissapear. I am led to believe that this is because the page is doing a postback and therefore the replies are lost because they are dynamically created. When the button is clicked, I ideally want to call an event in the code behind, using the 'Add Handler' method.

Does anyone have any ideas how I might be able to get this working, or if it seems possible to get it to work?

Many Thanks in Advance.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think you have written code for generating dynamic controls within IsPostBack ! If it's so then just write your code for the same outside of IsPostBack...... it will work !


Thanks for the reply, but my code has not been written inside an IsPostBack. Does anyone have anymore ideas on this?
Most Valuable Expert 2012
Top Expert 2008

Thre are too many possibilities, and without any more detail, it would be difficult to help...
I managed to solve the problem myself. I put a second datalist inside the ItemTemplate of the first datalist. This way, I now have a nested datalist and do not need to create the buttons .etc... dynamically.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial