Similar questions to this one have been asked but none seem to address my exact situation here's what I am trying to do.
有人提出了类似的问题,但似乎没有一个问题可以解决我的确切情况,这就是我要做的事情。
I have a user control that manages student info. i.e. FirstName, LastName, Address etc.
我有一个管理学生信息的用户控件。即FirstName,LastName,Address等。
I have a webpage/form that has a button on it. "Add Student". What I want to accomplish is for a new StudentInfo control to be added to the webform after each click.
我有一个网页/表单,上面有一个按钮。 “添加学生”。我想要完成的是每次点击后将新的StudentInfo控件添加到webform。
My current code looks something like this
我当前的代码看起来像这样
Private Sub btnAddStudent_Click(sender as object, ByVal e As System.EventArgs)
Dim lStudentInfo as Control
LoadControl("~/StudentInfo.ascx")
Me.placeholder1.controls.add(lStudentInfo)
End Sub
With this code only one StudentInfo control is added and upon pressing the "Add" button again a new StudentInfo control isnt added below the first one and the text/data entered within the first control is cleared.
使用此代码,仅添加一个StudentInfo控件,再次按下“添加”按钮时,不会在第一个控件下方添加新的StudentInfo控件,并清除在第一个控件中输入的文本/数据。
Thanks in advance for any assistance.
在此先感谢您的任何帮助。
1 个解决方案
#1
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.
发生的事情是,每次你做回发你以前的控制都会丢失。请记住,每个回发都使用页面类的全新实例。上次添加控件的实例在http请求完成后就被销毁 - 可能在浏览器完成加载它的DOM之前。
If you want a control to exist for every postback you have to add it on every postback.
如果您希望每个回发都存在一个控件,则必须在每次回发时添加它。
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.
此外,如果您希望ViewState适用于控件,则需要在页面的Load事件之前添加它。这意味着在Init或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
#1
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.
发生的事情是,每次你做回发你以前的控制都会丢失。请记住,每个回发都使用页面类的全新实例。上次添加控件的实例在http请求完成后就被销毁 - 可能在浏览器完成加载它的DOM之前。
If you want a control to exist for every postback you have to add it on every postback.
如果您希望每个回发都存在一个控件,则必须在每次回发时添加它。
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.
此外,如果您希望ViewState适用于控件,则需要在页面的Load事件之前添加它。这意味着在Init或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