Reputation:
Hopefully the title is not too confusing, but i've run into a problem and 2 days of googling hasn't helped.
I'm very new to ASP.NET and serverside in general and i've been trying to make a dynamic form. basically my form requires a user input from a drop down to select how many forms should be generated, then they'll click a button and the forms will be generated.
I've currently got it working how i want it to for the moment, but i can't help but think there's a better way of wrapping the individual form elements in a div (so i can style it as though they look seperate, i.e. add background, padding and margin)
At the moment i'm generating the start tag and end tag as literals.
Here's the markup
<form id="frmMyPge" runat="server">
<fieldset><div><asp:Label ID="Label1" AssociatedControlId="txtNumber" runat="server" >How many guests?</asp:Label></div>
<asp:DropDownList ID="txtNumber" runat="server">
<asp:ListItem Text="Select a value" Value="0" />
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
<asp:ListItem Text="5" Value="5" />
<asp:ListItem Text="6" Value="6" />
<asp:ListItem Text="7" Value="7" />
<asp:ListItem Text="8" Value="8" />
<asp:ListItem Text="9" Value="9" />
<asp:ListItem Text="10" Value="10" />
</asp:DropDownList>
<asp:Linkbutton id="butOK" Type="Submit" OnClick="SubmitBtn_Click" CssClass="btn" runat="server">Submit Request</asp:Linkbutton></fieldset>
<asp:panel ID="gnrtdstff" runat="server">
</asp:panel>
and here's the code behind:
Partial Class webresponse_Default
Inherits System.Web.UI.Page
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim i As Integer
For i = 1 To txtNumber.Text
'start fieldset
Dim strtfldst = New LiteralControl
strtfldst.Text = "<div>"
gnrtdstff.Controls.Add(strtfldst)
'generate first name label
Dim fnameLbl = New Label
fnameLbl.ID = "txtFnameLbl" & i
fnameLbl.Text = "First name: "
gnrtdstff.Controls.Add(fnameLbl)
'generate first name textbox
Dim fname = New TextBox
fname.ID = "txtFname" & i
gnrtdstff.Controls.Add(fname)
'generate last name label
Dim lnameLbl = New Label
lnameLbl.ID = "txtLnameLbl" & i
lnameLbl.Text = "Last name: "
gnrtdstff.Controls.Add(lnameLbl)
'generate last name textbox
Dim lname = New TextBox
lname.ID = "txtLname" & i
gnrtdstff.Controls.Add(lname)
'end fieldset
Dim endfldst = New LiteralControl
endfldst.Text = "</div>"
gnrtdstff.Controls.Add(endfldst)
Next
End Sub
End Class
Upvotes: 0
Views: 1353
Reputation: 13122
This seems like a good time to use a Repeater.
With a repeater you can create templates for each item or alternating item. There are many other useful templates. This will allow you to continue to use asp.net controls easily or not depending on your needs. It's also much easier to read then generating the page on your own in the code behind.
Once you've set up the templates you could generate the forms simply by calling databind, here's an example:
RepeaterControl.Datasource = Enumerable.Range(1, txtNumber.SelectedValue)
RepeaterControl.Databind
Edit:
Here is an example repeater with template:
<asp:Repeater ID="RepeaterControl" runat="server">
<ItemTemplate>
<div>
<asp:Label ID="txtFnameLbl" Text="First name:" runat="server" />
<asp:TextBox runat="server" ID="txtFname" />
<asp:Label ID="txtLnameLbl" Text="Last name:" runat="server" />
<asp:TextBox runat="server" ID="txtLname" />
</div>
</ItemTemplate>
</asp:Repeater>
For completeness here's an example of how you could get data out of the repeater once the form is submitted:
For Each ri As RepeaterItem In RepeaterControl.Items
fName = CType(ri.FindControl("txtFname"), TextBox).Text
'....
Next
Upvotes: 1
Reputation: 411
personally, i would just use a UserControl
: http://msdn.microsoft.com/en-us/library/system.web.ui.usercontrol.aspx for the whole thing (see the code at the bottom for an example, also see the 'Remarks' section for more usage-case examples).
This will allow you to contain all your elements neatly inside one container.
<%@ Control Language="VB"
A UserControl
can have it's own separate Validators, Postback, Scripts, etc.
Upvotes: 0
Reputation: 22619
Make use of HtmlGenericControl
Dim containerControl= New HtmlContainerControl("div")
//add all controls in a containerControl
containerControl.Controls.Add(myControl)
gnrtdstff.Controls.Add(containerControl)
Upvotes: 0
Reputation: 3618
If I know the items need to be on the form, just initially not visible, I tend to use the visibility property on controls. So, for example, you could add a textbox for firstname, and just set visible=false. Then, in your codebehind set the visibility property to true. This gives you the ability to have server side events for these controls, if needed, and also give you "design time" support, rather than worrying about building and rending code to see how things are working.
Furthermore, if you have a "group" of items that you want hidden, rather than using visible on each individual control, you can just wrap them all in a div, add a runat=server to the div, and then set the visibility style on that div. Example:
<div id="myDiv" runat="server">
<asp:textbox id="txtFname" runat="server"/>
<asp:textbox id="txtLname" runat="server"/>
</div>
Now, in your codebehind, you can simply say
Me.myDiv.Visible = True\False
Upvotes: 0