I have code to dynamically add more text boxes when the
user clicks the 'add another carrier' button. Problem is
it creates the text boxes within the same table row as
previously created boxes. It should create in new table
row. Code is below and if you see anything else you would
change please provide for optimization. And Thanks.
Code below.
<script runat="server">
Private Sub Page_Load
Dim intCounter As Integer
If not ispostback then
ViewState("FieldCount") = 1
'response.write ( ViewState("FieldCount") )
page1.Visible = True
page2.Visible = False
page3.Visible = False
btnPage1.Visible = False
btnPage3.Visible = False
btnPage4.Visible = False
ElseIf ViewState("FieldCount") > 1 then
response.write ( ViewState("FieldCount") )
For intCounter = 2 to ViewState("FieldCount")
AddField( intCounter)
Next
end if
End Sub
Sub AddFieldCount( s As Object, e As EventArgs)
ViewState("FieldCount") += 1
AddField( ViewState("FieldCount"))
End Sub
Sub AddField(strFieldNum As String)
Dim litLabel As LiteralControl
Dim txtTextBox As TextBox
strFieldNum = strFieldNum + 2
litLabel = New LiteralControl
litLabel.Text = "<p> " & strFieldNum & ":"
plhFields.Controls.Add( litLabel )
txtTextBox = New TextBox
txtTextBox.ID = "p2Carrier" & strFieldNum
plhFields.Controls.Add( txtTextBox )
txtTextBox = New TextBox
txtTextBox.ID = "p2PolicyNum" & strFieldNum
plhFields2.Controls.Add( txtTextBox )
txtTextBox = New TextBox
txtTextBox.ID = "p2DateM" & strFieldNum
txtTextBox.Columns="2"
plhFields3.Controls.Add( txtTextBox )
txtTextBox = New TextBox
txtTextBox.ID = "p2DateD" & strFieldNum
txtTextBox.Columns="2"
plhFields4.Controls.Add( txtTextBox )
txtTextBox = New TextBox
txtTextBox.ID = "p2DateY" & strFieldNum
txtTextBox.Columns="4"
plhFields5.Controls.Add( txtTextBox )
End Sub
</script>
<!--Second page panel-->
<asp:Panel id="page2" visible="true" runat="server">
Page 2 of 3
<table border="1" width="100%">
<tr>
<td colspan="3">
List any past carriers
</td>
</tr>
<tr>
<td>Carrier</td><td>Policy Number</td><td>Expiration
Date</td>
</tr>
<tr>
<td>1.<asp:textbox id="p2Carrier1" runat="server"/></td>
<td><asp:textbox id="p2PolicyNum1" runat="server"/></td>
<td><asp:textbox id="p2DateM1" runat="server"
Columns="2"/>/<asp:textbox id="p2DateD1" runat="server"
Columns="2"/>/<asp:textbox id="p2DateY1" runat="server"
Columns="4" /></td>
</tr>
<tr>
<td>2. <asp:textbox id="p2Carrier2" runat="server"/></td>
<td><asp:textbox id="p2PolicyNum2" runat="server"/></td>
<td><asp:textbox id="p2DateM2" runat="server"
Columns="2"/>/<asp:textbox id="p2DateD2" runat="server"
Columns="2"/>/<asp:textbox id="p2DateY2" runat="server"
Columns="4" /></td>
</tr>
<tr>
<td>3. <asp:textbox id="p2Carrier3" runat="server"/></td>
<td><asp:textbox id="p2PolicyNum3" runat="server"/></td>
<td><asp:textbox id="p2DateM3" runat="server"
Columns="2"/>/<asp:textbox id="p2DateD3" runat="server"
Columns="2"/>/<asp:textbox id="p2DateY3" runat="server"
Columns="4" /></td>
</tr>
<tr>
<td><asp:PlaceHolder id="plhFields" runat="server"/></td>
<td><asp:PlaceHolder id="plhFields2"
runat="server" /></td>
<td><asp:PlaceHolder id="plhFields3" runat="server"/> /
<asp:PlaceHolder id="plhFields4" runat="server"/> /
<asp:PlaceHolder id="plhFields5" runat="server"/> </td>
</tr>
</table>
<asp:button
text="Click here to add another carrier"
OnClick="AddFieldCount"
runat="server"
/>
</asp:Panel>