c# - Tips on generating dynamic form fields from a string of data? -


so here's want do. want take list of phrases, , in parts of phrases, indicate user input required.. dropdown list, textbox, date field, etc. kind of madlibs in way.. option of selecting things list.

for example:

i own ?list_of_cars?, purchased on ?date?. has ?freetext? miles on it.

this string gets broken apart mixture of text , html form fields/asp.net controls.

?list_of_cars? gets turned dropdown list few options choose from.

?date? gets turned date field (custom user control created)

?freetext? textbox user can enter information into.

i have 50 of these phrases need work with. end result end being plain text in textbox after user fills out required input.

no lists need large number of options.. cars kind of bad example. options list contain 5 probably.

at point, started storing entire string number of delimiters , placeholders database table.

for example, 1 row looks this:

i own {list^nissan,ford,chevy}, purchased on {date}. has {text} miles on it.

then break down , replace placeholders form fields.. put pure text. feel there might smarter/better way this. i'm not opposed backing , changing how initial phrases stored.

just curious if might have tips/suggestions?

here details different aspects you’ll have think of

storing lists

i’d suggest create 2 tables list (listid, listname) , listitem (listid, itemname) , keep data in database sake of easier maintenance. if have several lists of cars equal need update later make things easy you. on other hand updating several strings not easy this.

tags

these need made easy parse. i’d avoid complex names such “{list^nissan,ford,chevy}” , define ?list_id?. tags need identifiable. maybe ?tag_name? not idea because you’ll have questions marks might make parsing more difficult. better use #!tag_name!#

asp.net

all controls need added oninit method, not later in page_load.

parsing

here started needs more work.

private const string open_tag = "!#"; private const string close_tag = "#!";  private void parse(string s) {             int tagopenindex = 0;     int tagcloseindex = 0;      tagopenindex = s.indexof(open_tag);      string tag = string.empty;      while (tagopenindex >= 0)     {          //write before current tag opening          response.write(s.substring(tagcloseindex, tagopenindex));          //find current tag closing          tagcloseindex = s.indexof(close_tag, tagopenindex);          //get tag name         tag = s.substring(tagopenindex, tagcloseindex - tagopenindex + 1);          //parse tag , create asp.net controls. let's text box         //you'll need figure out put , such         //and how keep track of different control ids can use these later.         textbox t = new textbox();         t.id = "generate id";         form.controls.add(t);          //get index of next open tag         tagopenindex = s.indexof(open_tag, tagcloseindex);     } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -