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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic form textboxes

Status
Not open for further replies.

lisat76

Programmer
Sep 25, 2007
89
0
0
US
I have a site I am working on, a user will choose how many rooms a site has, then once they choose how many rooms there are in each of those rooms they must then enter how many chairs will go into each particular room.

I want to make a form where first the user puts how many rooms there are and then clicks a button which will then generate a number of textboxes for each room.
So if they enter 10 rooms then 10 teextboxes show up so they can then enter how many tables for each room.

Then once they fill that out the info will then be put into the database, I'll have to use a for each loop for that, because each textbox will represent one row of data.

I know how to do a form and add eveything to the database.

Not sure how to best do this scenario. I prefer VB.Net for code.


Thanks
 
this would be one approach.
Code:
rooms: <asp:textbox id="rooms" />
chairs: <asp:textbox id="chairs" />
<asp:button id="generate" />
Code:
protected void generate_Click(...)
{
   var numberOfRooms = int.Parse(rooms.Text);
   var numberOfChairs = int.Parse(chairs.Text);
   var link = string.Format("page2.aspx?rooms={0}&chairs={1}", numberOfRooms, numberOfChairs);
   Response.Redirect(link, false);
}
Code:
var ChairsTemplate[] templates;
protected override void Init(EventArgs e)
{
    var rooms = int.Parse(Request.QueryString["rooms"]);
    var chairs = int.Parse(Request.QueryString["chairs"]);
    templates = new ChairsTemplate[rooms];
    for(var r<0; r<rooms; r++)
    {
       var room = (chairstemplate)LoadControl("~/chairstemplate.ascx");
       room.NumberOfChairs = chairs;
       room.RoomIndex = r;
       Controls.Add(room);
       templates[r] = room;
    }
}

protected void Save_Click(...)
{
    foreach(var room in templates)
    {
        room.Save();
    }
}
Code:
<asp: repeater ID="ChairsLayout"/>
Code:
private TextBox[] Chairs;

public int NumberOfChairs {get;set;}
public int RoomIndex {get;set;}

public override void Init(EventArgs e)
{
   Chairs = new Textbox[NumberOfChairs];
   for(var c=0; c<NumberOfChairs;c++)
   {
       var chair = new TextBox();
       chair.ID = "Chair_"+c
       Chairs[c] = chair;
   }

   ChairsLayout.DataSource = Chairs;
   ChairsLayout.DataBind();
}

public void Save()
{
    Save the room/chairs within this user control.
}
this is off the top of my head, but it's a start. i'm sure there are syntax errors.
you could change the logic within chairstemplate.Init to build a specific number of chairs per row too.

this would work, however this is a very cumbersome user experience. I would look into some jqueryUI controls to create/drag/drop "chairs" into a "room" to give the user a more visual appeal.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top