我有一个相对简单的ASP.NET问题(我应该想到),令人遗憾的是我无法自己解决。我想要做的是以下几点:
但是,在下面的vb.net语句中运行时,“test”字符串永远不会进入文本框 - 任何有关解决此问题的帮助都将非常感激。
Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Controls_Load()
End Sub
Public Sub Controls_Load()
Dim ttf_tb As New TextBox With {.ID = "ttf_tb"}
Master_Panel.Controls.Add(ttf_tb)
Dim ttf_button As New Button
Master_Panel.Controls.Add(ttf_button)
AddHandler ttf_button.Click, AddressOf TTF_BUTTON_CLICK
End Sub
Public Sub TTF_BUTTON_CLICK(sender As Object, e As EventArgs)
Dim str As String = sender.id
Dim panel As New Panel
panel.ID = "TTF_Panel"
panel.Width = 300
panel.Height = 300
Master_Panel.Controls.Add(panel)
panel.BackColor = Drawing.Color.Black
panel.Style.Add(HtmlTextWriterStyle.Position, "absolute")
panel.Style.Add(HtmlTextWriterStyle.Left, "200px")
panel.Style.Add(HtmlTextWriterStyle.Top, "100px")
panel.Style.Add(HtmlTextWriterStyle.ZIndex, "100")
Dim CL_Button As New Button
CL_Button.ID = "TTF_Close_" & Replace(str, "TTF_Button_", "")
panel.Controls.Add(CL_Button)
AddHandler CL_Button.Click, AddressOf TTF_Close_Button_Click
End Sub
Public Sub TTF_Close_Button_Click(sender As Object, e As EventArgs)
Dim ttf_tb As TextBox = Master_Panel.FindControl("ttf_tb")
ttf_tb.Text = "Test"
Dim panel As Panel = FindControl("TTF_Panel")
Master_Panel.Controls.Remove(panel)
End Sub
End Class
答案 0 :(得分:2)
我认为您需要在Page_Init方法中重新创建控件。我已经有一段时间了,我已经完成了网络表单,但我认为它类似于:
当用户点击该按钮时,会触发回发帖。这将重新创建您的类的新实例,在页面上创建控件,分配任何表单值,然后调用您的Page_Load事件。
问题是您创建控件的时间太晚,因此表单值永远不会正确分配。
答案 1 :(得分:1)
您应该在Init事件中创建/重新创建动态控件:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Controls_Load()
End Sub
这应该允许你在PostBacks中维持他们的状态。
有关此主题的详细信息,请参阅the ASP.NET Page Life Cycle上的MSDN文章。