Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim text1 As New TextBox If Not IsPostBack Then
text1.Text = "DB"
text1.ID = "txt1"'无法指定Name
Me.Controls.Add(text1 )
End If
End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'这样完全可以,但是有很大的局限性,具体的跟页面的复杂程度有关,不详细描述。
Dim req As HttpRequest = System.Web.HttpContext.Current.Request
Response.Write(req.Form("txt1"))
'这样却不可以。但却是我想要的。
Dim s As TextBox = CType(FindControl("txt1"), TextBox)
Response.Write(s.Text)
End Sub
大概意思就是这样。强调一下下,动态添加只希望在页面加载时执行。说白了,不许写再If Not IsPostBack Then外面。
我该怎么再Button1_Click时接到这个值呢?谢谢!·!·!
详细如下:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim text1 As New TextBox If Not IsPostBack Then
text1.Text = "DB"
text1.ID = "txt1"'无法指定Name
Me.Controls.Add(text1 )
ViewState("txt1ViewState") = text1.Text
End If
End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'这样完全可以,但是有很大的局限性,具体的跟页面的复杂程度有关,不详细描述。
Dim req As HttpRequest = System.Web.HttpContext.Current.Request
Response.Write(req.Form("txt1"))
'这样却不可以。但却是我想要的。
Dim s As TextBox = CType(FindControl("txt1"), TextBox)
Response.Write(s.Text)
'Viewstate
Response.Write(ViewState("txt1ViewState"))'这里存放的永远是Text1变化前的数据,变化后的取不到。
End Sub
Get
Dim dynamic As Object = ViewState("IsDynamicLoadControl")
Return IIf(dynamic Is Nothing, False, True)
End Get
Set(ByVal value As Boolean)
ViewState("IsDynamicLoadControl") = value
End Set
End Property
Protected Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState) If IsDynamicLoadControl Then
LoadTextBox()
End If
End Sub Private Sub LoadTextBox()
For i As Integer = 0 To 1
Dim input As New TextBox()
input.ID = "Txt" + i.ToString()
Me.form1.Controls.Add(input)
Next
IsDynamicLoadControl = True
End Sub Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadTextBox()
Else
Dim s As TextBox = CType(FindControl("Txt0"), TextBox)
Response.Write(s.Text)
End If
End Sub