更新时间:2022-06-15 22:25:52
正在发生的事情是,每次你做一个回发你的previous控制时间丢失了。记住,每一个回发使用您的网页类的全新实例。实例添加的控制,最后一次是的摧毁的尽快http请求完成—可能是浏览器之前,甚至完成加载它的DOM。
What is happening is that every time you do a postback your previous control was lost. Remember, every postback uses a brand new instance of your page class. The instance you added the control to last time was destroyed as soon as the http request finished — possibly before the browser even finished loading it's DOM.
如果你想有一个控制为每一个回传存在,您必须添加它的在每个回发的。
If you want a control to exist for every postback you have to add it on every postback.
此外,如果你想的ViewState为控制工作,你需要在Load事件之前增加它的为页的。这意味着,init或preINIT无论是。
Additionally, if you want ViewState to work for the control you need to add it before the Load event for the page. This means either on Init or PreInit.
Private Sub btnAddStudent_Click(sender as object, ByVal e As System.EventArgs)
Me.placeholder1.controls.add(LoadControl("~/StudentInfo.ascx"))
Session("NewStudentControls") += 1
End Sub
Protected Sub Page_Init(sender as object, e as system.eventargs)
For i As Integer = 1 To Session("NewStudentControls")
Me.placeholder1.controls.add(LoadControl("~/StudentInfo.ascx"))
Next
End Sub