每按一次按钮,我都会尝试动态创建一个新文本框。页面要求是让用户在尽可能多的文本框中输入信息,因此我不能将一定数量的PlaceHolders抛出到页面上,然后动态地向其添加控件。
到目前为止,我可以将控件添加到页面上已有的占位符,但是当我点击我的"添加另一个"按钮,它只是用新的控件替换该控件,而不是在它下面添加它。
我已经尝试了各种方法来解决这个问题,而这正是我(在VB中)的目标:
'This is to be used in duplicating the PLO textbox within the panel
Dim newTextboxPLO As New TextBox() 'make a new textbox
Dim newLabelPLO As New Label() 'make a new label
Dim plhNewPlaceHolder As New PlaceHolder() 'make a new place holder
'Add a spacer
Dim spacer As LiteralControl = New LiteralControl("<br />")
ploCounter += 1
'New place holder attributes
plhNewPlaceHolder.ID = "plhNewPlaceHolder" & ploCounter
'New textbox attributes
newTextboxPLO.ID = "txtPLO" & ploCounter
newTextboxPLO.TextMode = TextBoxMode.MultiLine
newTextboxPLO.Width = "318"
newTextboxPLO.Height = "70"
'New label attributes
newLabelPLO.Text = (ploCounter) & ") "
newLabelPLO.Font.Bold = True
newLabelPLO.ID = "lblPLO " & (ploCounter)
pnlAddPLO.Controls.Add(plhNewPlaceHolder)
'If the original place holder has content in it, add the new textbox and label into a new placeholder
If plhPLO.HasControls = False Then
'Add new textbox and label into the placeholder
plhPLO.Controls.Add(newLabelPLO)
plhPLO.Controls.Add(newTextboxPLO)
plhPLO.Controls.Add(spacer)
Else
'Add new placeholder into the panel
pnlAddPLO.Controls.Add(plhNewPlaceHolder)
'Add items into the new placeholder
plhNewPlaceHolder.Controls.Add(newLabelPLO)
plhNewPlaceHolder.Controls.Add(newTextboxPLO)
plhNewPlaceHolder.Controls.Add(spacer)
'Increase the place holder counter
plhCounter += 1
End If
我已经检查了很多问题,包括this one,但它并没有提供很多帮助。我觉得我有一些小的东西,但是我不知道是什么。
答案 0 :(得分:0)
您在页面上始终呈现一个PlaceHolder的原因是,asp按钮单击始终会触发页面回发(刷新)。因此,您的页面总是会有一个占位符控件,即您的aspx文件中指定的控件。在this帖子的启发下,我建议你做的是将占位符存储在列表中,将该列表存储在会话变量中,然后在加载时加载页面上存储的占位符。下面是一个示例,考虑到页面加载时没有空占位符:
Private lstPlaceHolders As New List(Of WebControls.PlaceHolder)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Session("myPlaceholders") IsNot Nothing) Then
Me.lstPlaceHolders = Session("myPlaceholders")
' Add your stored placeholders one by one
For Each placeHolder As WebControls.PlaceHolder In lstPlaceHolders
pnlAddPLO.Controls.Add(placeHolder)
Next
End If
End Sub
Protected Sub btnNewPlaceholder_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNewPlaceholder.Click
'This is to be used in duplicating the PLO textbox within the panel
Dim newTextboxPLO As New TextBox() 'make a new textbox
Dim newLabelPLO As New Label() 'make a new label
Dim plhNewPlaceHolder As New WebControls.PlaceHolder() 'make a new place holder
'Add a spacer
Dim spacer As LiteralControl = New LiteralControl("<br />")
' Your placeholder counter always resets on every Postback, but the new placeholder's number will always be max + 1
Dim newPlaceHolderNumber As Integer = Me.lstPlaceHolders.Count + 1
'New place holder attributes
plhNewPlaceHolder.ID = "plhNewPlaceHolder" & newPlaceHolderNumber
'New textbox attributes
newTextboxPLO.ID = "txtPLO" & newPlaceHolderNumber
newTextboxPLO.TextMode = TextBoxMode.MultiLine
newTextboxPLO.Width = "318"
newTextboxPLO.Height = "70"
'New label attributes
newLabelPLO.Text = (newPlaceHolderNumber) & ") "
newLabelPLO.Font.Bold = True
newLabelPLO.ID = "lblPLO " & (newPlaceHolderNumber)
'Add items into the new placeholder
plhNewPlaceHolder.Controls.Add(newLabelPLO)
plhNewPlaceHolder.Controls.Add(newTextboxPLO)
plhNewPlaceHolder.Controls.Add(spacer)
lstPlaceHolders.Add(plhNewPlaceHolder)
' Add the placeholder in your panel here too, since the load event is always triggered before your btnClick event on a Postback
' and you don't want to refresh the page to see your new placeholder.
pnlAddPLO.Controls.Add(plhNewPlaceHolder)
Session("myPlaceholders") = lstPlaceHolders
End Sub
Protected Sub btnClearList_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClearList.Click
Session.Remove("myPlaceholders")
pnlAddPLO.Controls.Clear()
End Sub
如果您不希望每次在会话变量持续的20分钟(默认值)内返回此页面时显示占位符,请不要忘记清除会话变量。在我的代码中,我添加了一个清晰的按钮,但也许这不是你想要的。