ユーザーがフォームに複数のメールアドレスを追加できるページがあります。[追加]をクリックして、ajaxを介して別の動的テキストボックスを追加します。[保存]をクリックすると、テキストボックスに値が保存されますが、空白のテキストボックスがある場合は、保存時に削除します。
それを削除するコードがあり、コントロールがプレースホルダーにないことを確認しましたが、これを表示するようにページが更新されていないようです。
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If(Not Page.IsPostBack)
AddEmailAddressBoxesLoad()
Else
Dim button As Control = GetPostBackControl(Me.Page)
If (button IsNot Nothing) Then
If (button.ID = "btnAdd") Then
AddEmailAddressBoxes()
ElseIf (button.ID.Contains("btnSave"))
AddEmailAddressBoxesSave()
RemoveTextBox()
End If
End If
End If
End Sub
Public Function RemoveTextBox() As List(Of String)
Dim emptyTextBoxList = New List(Of TextBox
)
For Each control As Control In phMultiEmailAddressBoxes.Controls
If(TypeOf(control) Is TextBox)
Dim textBox = CType(control, TextBox)
If(String.IsNullOrEmpty(textBox.Text))
emptyTextBoxList.Add(textBox)
End If
End If
Next
For Each textBox As TextBox In emptyTextBoxList
phMultiEmailAddressBoxes.Controls.Remove(textBox)
Next
End Sub
TextBoxメソッドを追加します
Private Sub AddEmailAddressBoxes()
Dim count As Integer
If (ViewState("EmailAddressboxCount") IsNot Nothing)
count = CType(ViewState("EmailAddressboxCount"), Integer)
End If
count = count + 1
ViewState("EmailAddressboxCount") = count
For i As Integer = 1 To count
AddTextBox(i)
Next
End Sub
Private Sub AddEmailAddressBoxesLoad()
Dim count As Integer = 1
If(TrustSettings.HREmailAddressList.Any())
count = TrustSettings.HREmailAddressList.Count()
End If
ViewState("EmailAddressboxCount") = count
For i As Integer = 1 To count
AddTextBox(i)
Next
End Sub
Private Sub AddTextBox(i As Integer)
Dim divStart = new Literal()
divStart.Text = "<div>"
phMultiEmailAddressBoxes.Controls.Add(divStart)
Dim textBox = New TextBox()
textBox.ID = "txtEmailBox" + i.ToString()
textBox.MaxLength = 200
textBox.CssClass = "email"
If(TrustSettings.HREmailAddressList.Any() And TrustSettings.HREmailAddressList.Count() >= i)
textBox.Text = TrustSettings.HREmailAddressList(i-1)
End If
phMultiEmailAddressBoxes.Controls.Add(textBox)
Dim divEnd = new Literal()
divEnd.Text = "</div>"
phMultiEmailAddressBoxes.Controls.Add(divEnd)
End Sub
パネルマークアップを更新
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:PlaceHolder ID="phMultiEmailAddressBoxes" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
<asp:AsyncPostBackTrigger ControlID="btnSave" />
</Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnAdd" Text="Add new email address" CssClass="but_small_png" UseSubmitBehavior="False" />
<asp:Button runat="server" ID="btnSave" Text="Save" CssClass="but_small_png" UseSubmitBehavior="False" />
編集:@YuriyRozhovetskiyのコードがVBに移植されました
protected sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
if (IsPostBack)
for each value As String in DynamicTextBoxes
Dim tb = new TextBox() with { .ID = value, .CssClass = "email" }
phMultiEmailAddressBoxes.Controls.Add(tb)
Next
End If
End Sub
private Property DynamicTextBoxes() As List(Of string)
Get
Dim list = ViewState("DynamicTextBoxes")
if (list Is Nothing)
list = New List(Of string)()
ViewState("DynamicTextBoxes") = list
End If
return list
End Get
Set
End Set
End Property
protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
AddHandler btnSave.Click, AddressOf btnSave_Click
AddHandler btnAdd.Click, AddressOf btnAdd_Click
End Sub
Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
For Each value As string In DynamicTextBoxes
Dim tb = CType(phMultiEmailAddressBoxes.FindControl(value), TextBox)
if (tb IsNot Nothing And string.IsNullOrEmpty(tb.Text))
phMultiEmailAddressBoxes.Controls.Remove(tb)
DynamicTextBoxes.Remove(value)
End If
Next
End Sub
Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
Dim value = "tbEmailAddress" + DynamicTextBoxes.Count.ToString()
Dim tb = new TextBox() with { .ID = value, .CssClass = "email" }
phMultiEmailAddressBoxes.Controls.Add(tb)
DynamicTextBoxes.Add(id)
End Sub