0

我有一个带有下拉列表的表单的面板,其值为 1-10。当按下面板上的按钮时,该面板将变为不可见,而第二个则变为可见。在第二个面板上,我动态生成了多行控件。行数由下拉列表中的选定项确定。

我的问题是,当我想执行验证然后存储动态创建的控件内容的值时,控件被删除。

我知道这与页面的生命周期和动态创建的控件的持久性有关,但我不知道要解决这个问题。我在这里阅读了许多其他类似的问题,并且很少有谁来解决这个问题的工作示例,所以我一点也不聪明。这包括需要在 pageload 或 pageinit 上创建动态创建的控件的建议,因为我们不知道要创建多少,这怎么可能?

回顾一下:页面加载时不会添加这些控件,这些控件仅在用户按下按钮后添加。在用户选择他们想要的数量之前,我们不知道要制作多少个控件。

到目前为止我的代码:

Partial Class View
Inherits SeatPlannerModuleBase
Implements IActionable
Dim valid As String = "success"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
    Try

        Dim n As Integer = 0

        ' now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
        For i As Integer = 0 To n - 1
            res_Placeholder.Controls.Add(New Label())
            booking_Placeholder.Controls.Add(New Label())

            title_Placeholder.Controls.Add(New TextBox())
            firstname_Placeholder.Controls.Add(New TextBox())
            surname_Placeholder.Controls.Add(New TextBox())
            ticketNum_Placeholder.Controls.Add(New TextBox())

        Next

        ' now, set the Text property of each TextBox
        IterateThroughBooking(Me)
        IterateThroughReservation(Me)
        IterateThroughTitle(Me)
        IterateThroughFirstname(Me)
        IterateThroughSurname(Me)
        IterateThroughTicketNum(Me)

        If Not Page.IsPostBack Then

        End If

    Catch exc As Exception        'Module failed to load
        ProcessModuleLoadException(Me, exc)
    End Try
End Sub

Protected Sub IterateThroughTitle(ByVal parent As Control)
    Dim count As Integer = 1

    For Each c As Control In title_Placeholder.Controls
        If c.[GetType]().ToString().Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then
            'DirectCast(c, TextBox).Text = "TextBox_Title " + count.ToString()
            DirectCast(c, TextBox).ID = "TextBox_Title_" + count.ToString()
            DirectCast(c, TextBox).CssClass = "DYN_TextBox"
            count += 1
        End If

        If c.Controls.Count > 0 Then
            IterateThroughTitle(c)
        End If
    Next
End Sub

Protected Sub CreateTextBoxes()

    Dim n As Integer = number_of_tickets_Ddl.SelectedValue

    ' now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
    For i As Integer = 0 To n - 1
        res_Placeholder.Controls.Add(New Label())
        booking_Placeholder.Controls.Add(New Label())

        title_Placeholder.Controls.Add(New TextBox())
        firstname_Placeholder.Controls.Add(New TextBox())
        surname_Placeholder.Controls.Add(New TextBox())
        ticketNum_Placeholder.Controls.Add(New TextBox())

    Next

    ' now, set the Text property of each TextBox
    IterateThroughBooking(Me)
    IterateThroughReservation(Me)
    IterateThroughTitle(Me)
    IterateThroughFirstname(Me)
    IterateThroughSurname(Me)
    IterateThroughTicketNum(Me)

    Retreive_first_row()

End Sub

Protected Sub next_submit_Btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles next_submit_Btn.Click

    Step1_Pnl.Visible = False
    Step2_Pnl.Visible = True
    CreateTextBoxes()
    Button1.Attributes.Add("OnClick", "validate_step2(); return false;")

End Sub

Protected Sub next_submit2_Btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles next_submit2_Btn.Click

    step2_error_Lbl.Text = CType(Me.FindControl("TextBox_Title_2"), TextBox).Text

End Sub

End Class       
4

1 回答 1

0

动态创建的控件在回发后不会持续存在,您必须每次都重新创建它们,这是一个技术限制。要知道创建了多少控件,您可以在 session/viewstate/whatever 中保存一个变量,如果您像以前的状态一样命名它们,则视图状态将被恢复。

检查这些链接以获取更多信息:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx

ASP.NET 动态创建控件和回发

于 2012-08-20T18:42:25.383 回答