<script runat="server">
Sub ddlNum_Changed(sender As Object, e As System.EventArgs)
Dim i As Integer
Dim txt As TextBox
Dim lblBr As Label
For i = 1 To Me.ddlNum.SelectedItem.Value
txt = New TextBox
With txt
.id = "txt" & i
End With
lblBr = New Label
lblBr.Text = "<br>"
Me.pnlCtrl.Controls.Add(txt)
Me.pnlCtrl.Controls.Add(lblBr)
Next
IF Me.ddlNum.SelectedItem.Value > 0 Then
Me.test.Visible = True
Else
Me.test.Visible = False
End IF
End Sub
Sub test_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer
Dim num As Integer = 0
For i = 0 To Me.ddlNum.SelectedItem.Value - 1
num += pnlCtrl.ID & i
Next
Me.lblText.Visible = True
Me.test.Visible = False
Me.ddlNum.Visible = False
Label1.Text = num.ToString
End Sub
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList id="ddlNum" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlNum_Changed">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
</asp:DropDownList>
<table border="1">
<tr>
<td><asp:Panel id="pnlCtrl" runat="server"></asp:Panel></td></tr>
</table>
<input id="test" Visible="false" type="button" OnServerClick="test_OnClick" value="ok" runat="server" />
' <asp:Label id="lblText" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
Dim i As Integer
Dim num As Integer = 0
Dim lhlText As TextBox
For i = 0 To Me.ddlNum.SelectedItem.Value - 1
lhlText = Me.FindControl("lbl" & var)
Label1.Text = lhlText.Text 'ตรงนี้
Next
คือผมก็ดัดแปลงจาก ASP.NET Upload & Create Control - FindControl แหละคับ
ตามที่พี่วินแนะนำมา มันก็ยังไม่อ่านค่าอะคับ
Code (VB.NET)
Sub ddlNum_Changed(sender As Object, e As System.EventArgs)
Dim i As Integer
Dim txt As TextBox
Dim lblBr As Label
For i = 1 To Me.ddlNum.SelectedItem.Value
txt = New TextBox
With txt
.id = "txt" & i
End With
lblBr = New Label
lblBr.Text = "<br>"
Me.pnlCtrl.Controls.Add(txt)
Me.pnlCtrl.Controls.Add(lblBr)
Next
End Sub
Sub test_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer
Dim lhlText As TextBox
For i = 0 To Me.ddlNum.SelectedItem.Value - 1
lhlText = Me.FindControl("txt" & i)
Label1.Text = lhlText.Text
Next
end sub
Dim i As Integer
For i = 0 To Me.ddlNum.SelectedItem.Value - 1
Dim strID As String = "txtz" & i
Dim ltext As TextBox = CType(pnlCtrl.FindControl(strID), TextBox)
Label1.Text = Label1.Text & ltext.Text & "<br/>" 'ตรงนี้คับ
Next