Dynamic inputs with ASP.NET

  • Thread starter Thread starter MscrmGuy
  • Start date Start date
M

MscrmGuy

Hi all:

I am trying to build a web interface for a CRM demo I am working on, and one
of the questions on the registration form is "How many children do you
have?" and then I want the user to be able to enter info for each child.

So I'd like the page to function such that if the user enters "4", the
interface updates and displays 4 input rows with textboxes for name, age,
etc. This seems to be hard to do in ASP.NET, does anyone think AJAX might be
an answer? Can anyone offer any other ideas as to how I might accomplish
this?

Thanks.
 
MscrmGuy said:
So I'd like the page to function such that if the user enters "4", the
interface updates and displays 4 input rows with textboxes for name, age,
etc. This seems to be hard to do in ASP.NET, does anyone think AJAX might
be an answer? Can anyone offer any other ideas as to how I might
accomplish this?

There are a few ways you could do this. Perhaps the easiest is to have your
'number of children' as a drop down control with autopostback, and also have
a hidden panel or repeater. On postback of the drop down you could
dynamically add the required number of textboxes, like so:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<br />
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True">
<asp:ListItem Selected="True">0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:Panel ID="Panel1" runat="server" Height="50px"
Visible="False" Width="125px">
</asp:Panel>
&nbsp;</div>
</form>


Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Panel1.Visible = True
Panel1.Controls.Clear()
For i As Integer = 1 To CInt(DropDownList1.SelectedValue)
Panel1.Controls.Add(New TextBox())
Next
End Sub

Then you just need to loop through Panel1.Controls to get the names.

HTH
 
Back
Top