Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2005, 09:19 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default Build a page dynamically

Hello all,

   This is what I have been tasked with doing, and I am sort of lost in how to do this. Bascially I need to create forms with questions on it for a user to answer. The DB is set up with the questions and the type of control that should be displayed for answers.
   I guess my confusion comes in on how to add controls(for the answers) dynamically. Also naming them so that they can be accessed from the codebehind. And if they are added dynamically, will events still work, ie.click, selectedindexchanges... etc.
   Any ideas and suggestions would be appreciated.

Thanks in advance,

Jim

 
Old June 15th, 2005, 10:57 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This is a tricky task. Dynamic control sets can cause lots of problems.

What I see happening is that you need to add all the controls (based on the database results) every time the page runs. This needs to be done early on (page_load might be ok, otherwise you might need to back up to Page_init or even the EnsureChildControls method). Once you have all the controls on the page, normal stuff should apply. The controls added on can be wired to each other if you have question dependancies, etc. Then events should behave.

A note about what I just described. We often see posts like "why doesn't my dropdownlist maintain state?" The replies often say, "because you are binding the control every time". In the case of what I described, the important difference is that you need to load the control versus bind the control's data. When you load all those controls, you'll reload the control structure as it was when the page was constructed and rendered the first time. When a new control is added to the page's controls collection, .NET will try to match it up with what's in viewstate and thus restore its state.

I worked for 4 years at a company that made extensive use of the so-called "question engine". Working with it was the new developer hazing ritual. As of the time I left, we still hadn't really come up with a great solution.

-Peter
 
Old June 15th, 2005, 11:09 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

   Hey Peter I was hoping you would reply. Thanks for the direction. I guess I am in the same boat you where. The term "question engine" is exactly what the have used. The idea of this sounds great, will allow the users to create their own surveys, but the coding for it just seems like it will be overwhelming.
   Since posting my question, I have been poking around the net, and I found some great articles on 4GuysFromRolla. This article inparticular is very interesting and informative.
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Thanks again, and I will be crossing my fingers I can get this done.

 
Old June 15th, 2005, 03:07 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I skimmed that article. Looks very interesting. I will have to read that more thoroughly at some point.

Good luck, I'm sure you can figure it out!

-Peter
 
Old June 16th, 2005, 10:27 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Peter,
  Another question. I am able to load controls dynamically. In testing this out I see the just get put next to each other like the page was in flow layout. I tried adding to a panel or place holder but with the same results. I guess I can always set the top and left properties, but that takes some of the flexibilty out of the code. How did you do it in your app. In other words, how did you lay out your controls to make them line up etc and look nice, when you don't know how many you will have?

Thanks for the help..

 
Old June 16th, 2005, 10:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

That, my friend, is one of those fabulous things about building a question engine.

You need to either establish a default layout (ex. 2 hard returns between each control) or you need to provide a means to define for each question how it will be layed out.

To get started you can just build it so you add a control, then add some literals ("<br><br>") after the question control. Then take it from there.

-Peter
 
Old June 16th, 2005, 10:54 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Ummm that's what I was afraid of. The problem is, if you have many questions, and I will, you will have one very long page.

Thanks for the help :)

Jim

 
Old June 17th, 2005, 08:18 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I suppose you could build the page with some limit on the number of displayed questions and build in paging.

Also, depending on how your typical question is layed out I think you could use the datalist control which allows for multi column layout. Or you'll just have to do some custom rendering or at least add in literal controls to handle formatting between each question.

-Peter
 
Old June 17th, 2005, 10:14 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Thanks. I am still playing around with some things. I will try your datalist idea. Sound like it might be just what I need.

 
Old June 17th, 2005, 11:32 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Peter,

   I am not familiar with the datalist. I have been trying to use it. The problem is in adding the controls dynamically. I get an error:Specified cast is not valid. Have you used the datalist in this way. I am begining to think you must layout controls at design time and bind them.

Thanks for any help,
Jim






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically include an aspx page ndkicha_1981 General .NET 0 October 19th, 2007 03:12 AM
how to dynamically change an HTML page kpnikhil PHP How-To 1 September 7th, 2007 03:53 AM
Build an array dynamically yeva40 Javascript How-To 1 May 27th, 2005 03:09 PM
aspx - dynamically expanding the page vhrao General .NET 1 November 5th, 2004 03:49 PM
Dynamically Build 1 Xtab Qry Based On 6 Xtab Qry's Rood67 Access 1 March 4th, 2004 06:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.