Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic control type and ID

Status
Not open for further replies.

NHW

Programmer
Mar 16, 2004
24
0
0
HK
I'm trying to create dynamic controls which type and ID depends on some row data retrieved from a database.

For example if the data column "productType" is "1", then create a textbox with ID the same as the column "productID".

I've tried using a Repeater. But the problem is there seems to be no way to retrieve the value of a particular field to a variable (instead of a data-binding element) and then set the ID of the textbox as the control is rendered. I searched around and seems like most solutions point to iterating the repeater, finding the control, and then setting the properties.. but it won't work in this case because I'm not sure how to find the appropriate control in the first place.

Thanks in advance!
 
how dynamic do you need the output to be? views (webpages) are very specific and therefore the data rendered should be specific. if you are trying to create a one-form-fits-all then I would rethink your approach. the data will change, but the structure shouldn't (at least not that much).

that said, using a repeater you can define the ItemTemplate with the appropriate structure. use Eval("") to bind the data to the controls.

repeaters are best used for read-only data. if you want preform inline edits, a formview or gridview is better.

if you want more control of the html then I would recommend MS MVC or Castle Monorail, not webforms.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks for the reply.

The scenario is I need to create 20 online surveys, and they all share the same structure (except the number of questions) in terms of answer types, validation etc.. So instead of making 20 aspx files I thought it might be easier to create one page, then feed the contents (ID, text, value, control type etc) from the database fields depending on which survey is being called up.

I've tried inserting a textbox id using the Eval() method in a repeater but seems like control ID has to be static. I guess I could use straight codes to iterate the number of questions and then build controls using the Controls.Add() method but I just wonder if there's an easier way to do it.. given there are so many databound controls in .net
 
I would say your current approach is working against the webforms framework.

I don't think creating 1 page that is fully dynamic would be a good solution, no matter what the html engine. however creating some of the content (like answer format) would make sense.

I would approach it like this.
have a page Questions.aspx. then have a user control for each type of answer (open text, yes/no, multiple choice, etc).
I would display one question at a time with an "X of Y questions" on the page so the user knows how many questions remain.

in the Init event I would determine what type of user control to dynamically add to the page depending on the type of answer the question was. (the question would have some type of identifier stating the type of answer to accept). you may also be able to use a Multiview for each question type. each view would have a usercontrol. so the multiview is just a thin wrapper around user controls.

now each answer type is encapsulated into a user control. and can produce an "Answer" object without the "Question" begin concerned with the mechanics (it's all contained within each user control.) And your webcontrols will have constant server ids so you don't need to mess with that.

each usercontrol would implement an interface like
Code:
public interface IAnswerConstructor
{
   public void AllowableAnswers(object choices);
   public Answer SelectedAnswer();
}
the question.aspx could pull the answer from the user control like this
Code:
var answer = ((IAnswerConstructor)myusercontrol).SelectedAnswer();
question.Answser = answer;
repository.Save(question);

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks for your advice. I shall play around with it more.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top