Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Dynamic generation of lots of controls?

Here's the deal.  Working in ASP.NET.  I want to pull data from a database and display a schedule for a period of time with associated data, like this:

05/25/04 (edit)
Data1 blah blah blah
Data2 bleh bleh bleh

05/26/04 (edit)
Data1 bluh bluh bluh
Data2 bloh bloh bloh

05/26/04 (edit)
Data1 blih blih blih
Data2 blyh blyh blyh

Currently I am generating the HTML programatically and slapping it into a Label control.  It looks good, but what I would like to do, no, must do, is make those little (edit) texts to be links to another page, where Data1 and Data2 can be edited.  Presently there are no links. 

Naturally, the (edit) link for 5/25/05 should somehow successfully pass which date we're editing when we go to the new page, i.e. the link should connect us with the proper record.

I have three possible approaches (I'm open to others):

1) Just generate the href link programattically, use GET encoding to make the date part of the link, and have the receiving page dig around in Page_Load and see if anything was passed through GET.  I'm fairly sure I can make this work, but using GET is not very much in the ASP.NET spirit, and I'd like to avoid this approach if possible.

2) Somehow use a Repeater control that generates a lot of Linkbuttons, perhaps using the Command property of those LinkButtons?  Something I read online suggested this as an approach, and people do swear by the Repeater, but I've not used it and don't know if it's really going to cut it for me.  The HTML I'm currently generating isn't simple, there's conditional formatting based on the data, etc, and I'm a little nervous that the repeating of the Repeater won't hold up to all the conditionals.

3) Just run a loop and generate new LinkButtons directly and add them to the form.  Somehow these would need to be intermingled with the rest of the data display, presumably I can generate lots of Labels or something to display the rest of my data.

Obviously I'm not very clear on the details of the last two.  I'm hoping someone has already solved a similar problem and has some suggestions.  Or, if anyone has a better idea than the approaches I mentioned, I'm game for that too.

Thanks for any advice.

Matt Conrad
Thursday, May 27, 2004

Following up my own post:

Played with the Repeater a bit and decided it was just going to be too hard to get it to do all the stuff I wanted it to do.  I'm honestly not that impressed with the Repeater.  I might have been able to spit out what I wanted by writing a template class, but maybe not.  It didn't look promising.

I ended up using #3, just generating the controls from a loop.  I created a set of Literals to catch the rest of the HTML that I wanted to display, and a LinkButton for each date.  Looped through and added all of these guys to a PlaceHolder inside a Panel.  Exactly what I wanted, looks good, works perfectly, I'm happy.

Matt Conrad
Thursday, May 27, 2004

*  Recent Topics

*  Fog Creek Home