Hello~
I have a question on getting data back from the controls generated during runtime.
The following code are which I have tried. when user click the button "enter Details", It will generate sets of controls for data entering. However, it could only show the interface of the newly generated controls, when I try to get back the user entered value from them after user clicked buttom "summit", the controls seems disappeared. So can not get back the values.
what's wrong with the code??
Also, is there any ways to build event handler for those generated buttons(p.s maybe many-many buttons, e.g the "test" button)
test.aspx.vb
the web form
I have a question on getting data back from the controls generated during runtime.
The following code are which I have tried. when user click the button "enter Details", It will generate sets of controls for data entering. However, it could only show the interface of the newly generated controls, when I try to get back the user entered value from them after user clicked buttom "summit", the controls seems disappeared. So can not get back the values.
what's wrong with the code??
Also, is there any ways to build event handler for those generated buttons(p.s maybe many-many buttons, e.g the "test" button)
test.aspx.vb
VB.NET:
Partial Class test
Inherits System.Web.UI.Page
Dim var As Integer = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnEnter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Me.PlInput.Visible = True
Me.btnSummit.Visible = True
Dim i As Integer
' get the value entered
var = Integer.Parse(Me.ddlChild.SelectedValue())
' inside PlInput, build the following things for input
For i = 1 To var
' build label
Dim t1 As New Label
t1.ID = "lbchild" + i.ToString
t1.Height = 1
t1.Text = "Child " + i.ToString + " : "
t1.Width = 64
Me.PlInput.Controls.Add(t1)
Me.PlInput.Controls.Add(New LiteralControl("<br>Name: "))
' build textbox
Dim t2 As New TextBox
t2.ID = "tbName" + i.ToString
Me.PlInput.Controls.Add(t2)
Me.PlInput.Controls.Add(New LiteralControl(" with "))
' build dropdownlist
Dim t3 As New DropDownList
t3.ID = "ddlVarNo" + i.ToString
t3.Width = 48
Dim t3_1 As New ListItem
t3_1.Selected = True
t3_1.Text = 1
t3_1.Value = 1
t3.Items.Add(t3_1)
Dim t3_2 As New ListItem
t3_2.Selected = False
t3_2.Text = 2
t3_2.Value = 2
t3.Items.Add(t3_2)
Dim t3_3 As New ListItem
t3_3.Selected = False
t3_3.Text = 3
t3_3.Value = 3
t3.Items.Add(t3_3)
Dim t3_4 As New ListItem
t3_4.Selected = False
t3_4.Text = 4
t3_4.Value = 4
t3.Items.Add(t3_4)
Me.PlInput.Controls.Add(t3)
Me.PlInput.Controls.Add(New LiteralControl(" variable(s)<br>"))
' build button
Dim t4 As New Button
t4.ID = "btntest" + i.ToString
t4.Text = "test"
Me.PlInput.Controls.Add(t4)
Me.PlInput.Controls.Add(New LiteralControl("<br>"))
Me.PlInput.Controls.Add(New LiteralControl("<br>"))
Next
End Sub
Protected Sub btnSummit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSummit.Click
Me.PlOutput.Visible = True
Dim i As Integer
Dim name As String
Dim tb As TextBox
Dim ddl As DropDownList
Dim varNo As Integer
' report the input data
For i = 1 To var
tb = Me.PlInput.FindControl("tbName" + i.ToString)
name = tb.Text
ddl = Me.PlInput.FindControl("ddlVarNo" + i.ToString)
varNo = Integer.Parse(ddl.SelectedValue)
Me.PlInput.Controls.Add(New LiteralControl(name + " has " + varNo + " variable(s)" + "<br>"))
Next
End Sub
End Class
HTML:
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
with
<asp:DropDownList ID="ddlChild" runat="server">
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
child
<asp:Button ID="btnEnter" runat="server" Text="Enter details" />
<asp:Panel ID="PlInput" runat="server" Height="136px" Width="560px" Visible="False">
</asp:Panel>
</div>
<asp:Panel ID="PlOutput" runat="server" Height="136px" Width="560px" Visible="False">
</asp:Panel>
<asp:Button ID="btnSummit" runat="server" Text="Summit" Visible="False" />
</form>
</body>
</html>
Last edited by a moderator: