0

我有一个问题,我的手风琴面板带有动态生成的手风琴窗格,以及动态生成的控件,例如我的“btnVoucherProcess”按钮。现在我正在尝试使用“查找控件”方法访问我的按钮(btnVoucherProcess),如下所示:

     For i As Int32 = 0 To acAccomodation.Panes.Count - 1
        Dim btnVoucherProcess As Button CType(acAccomodation.FindControl("btnVoucherProcess" & i), Button) 'New Button("btnVoucherProcess" & i)
        btnVoucherProcess.Text = "Saved " & i
    Next

如果我运行此代码会引发错误,该错误告诉我我的按钮为空。如何访问手风琴内的按钮 (btnVoucherProcess)。

提前致谢。

===========

这是整个代码 代码在page_load中调用

    The code that is called in page_load  Private Sub emptyDaysAddMode(ByVal duration As Integer)

    For i As Integer = 0 To duration - 1
        Dim pn As New AjaxControlToolkit.AccordionPane()
        pn.ID = "Pane" & i
        pn.HeaderContainer.Controls.Add(accomodation(i, "", "", "", txtMarkup.Text, 0, 0, 0, 0, 0, 0))
        pn.ContentContainer.Controls.Add(accomodationDetails(i))
        acAccomodation.Panes.Add(pn)
        'drRow(0) = i + 1
        'dtExcel.Rows.Add(drRow)
    Next
End Sub

Function accomodation(ByVal i As Int32, ByVal ID As String, ByVal dteDate As String, ByVal strDescription As String, ByVal Markup As Double, ByVal SGL As Int32, ByVal Twin As Int32, ByVal Triple As Int32, ByVal Child As Int32, ByVal Teen As Int32, ByVal ChildOR As Int32) As Table
    accomodation = New Table()

    Dim myTableRow As New TableRow
    myTableRow.Width = Unit.Percentage(100)
    Dim dayCell As New TableCell
    Dim txtDay As TextBox = New TextBox
    txtDay.ID = "txtDate" & i
    txtDay.Text = dteDate
    If txtDay.Text = "" And txtArrival.Text <> "" Then txtDay.Text = DateAdd("d", i, CType(txtArrival.Text, Date))
    txtDay.Width = Unit.Pixel(70)
    dayCell.Controls.Add(txtDay)

    Dim lblAccID As Label = New Label
    lblAccID.ID = "lblAccID" & i
    lblAccID.Text = ID
    lblAccID.Visible = False
    dayCell.Controls.Add(lblAccID)
    myTableRow.Cells.Add(dayCell)


    Dim hotelRateCell As New TableCell
    Dim txtHotelRate As TextBox = New TextBox
    txtHotelRate.ID = "txtHotelRate" & i
    txtHotelRate.Text = strDescription
    txtHotelRate.Width = Unit.Pixel(295)
    txtHotelRate.Attributes.Add("onkeyup", "javascript:Complete(this, event," & i & ")")
    hotelRateCell.Controls.Add(txtHotelRate)
    myTableRow.Cells.Add(hotelRateCell)

    Dim markupCell As New TableCell
    Dim txtmarkup As TextBox = New TextBox
    txtmarkup.ID = "txtMarkup" & i
    txtmarkup.Width = Unit.Pixel(35)
    txtmarkup.Text = Markup
    If txtmarkup.Text = 0 Then txtmarkup.Text = txtQuotationMarkup.Text
    txtmarkup.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    markupCell.Controls.Add(txtmarkup)
    myTableRow.Cells.Add(markupCell)

    Dim nightCell As New TableCell
    Dim txtnight As TextBox = New TextBox
    txtnight.ID = "txtnight" & i
    txtnight.Text = 1
    txtnight.Width = Unit.Pixel(25)
    txtnight.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    nightCell.Controls.Add(txtnight)
    myTableRow.Cells.Add(nightCell)

    Dim sglCell As New TableCell
    Dim txtSGL As TextBox = New TextBox
    txtSGL.ID = "txtSGL" & i
    txtSGL.Width = Unit.Pixel(25)
    txtSGL.Text = SGL
    txtSGL.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    sglCell.Controls.Add(txtSGL)
    myTableRow.Cells.Add(sglCell)

    Dim DBLCell As New TableCell
    Dim txtDBL As TextBox = New TextBox
    txtDBL.ID = "txtDBL" & i
    txtDBL.Width = Unit.Pixel(25)
    txtDBL.Text = Twin
    txtDBL.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    DBLCell.Controls.Add(txtDBL)
    myTableRow.Cells.Add(DBLCell)

    Dim trpCell As New TableCell
    Dim txtTRP As TextBox = New TextBox
    txtTRP.ID = "txtTRP" & i
    txtTRP.Width = Unit.Pixel(25)
    txtTRP.Text = Triple
    txtTRP.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    trpCell.Controls.Add(txtTRP)
    myTableRow.Cells.Add(trpCell)

    Dim chdCell As New TableCell
    Dim txtCHD As TextBox = New TextBox
    txtCHD.ID = "txtCHD" & i
    txtCHD.Width = Unit.Pixel(25)
    txtCHD.Text = Child
    txtCHD.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    chdCell.Controls.Add(txtCHD)
    myTableRow.Cells.Add(chdCell)

    Dim tnCell As New TableCell
    Dim txtTN As TextBox = New TextBox
    txtTN.ID = "txtTN" & i
    txtTN.Width = Unit.Pixel(25)
    txtTN.Text = Teen
    txtTN.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    tnCell.Controls.Add(txtTN)
    myTableRow.Cells.Add(tnCell)

    Dim childOWRCell As New TableCell
    Dim txtChildOWR As TextBox = New TextBox
    txtChildOWR.ID = "txtchildOWR" & i
    txtChildOWR.Width = Unit.Pixel(25)
    txtChildOWR.Text = ChildOR
    txtChildOWR.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
    childOWRCell.Controls.Add(txtChildOWR)
    myTableRow.Cells.Add(childOWRCell)

    Dim CostCell As New TableCell
    Dim txtCost As TextBox = New TextBox
    txtCost.ID = "txtCost" & i
    txtCost.Width = Unit.Pixel(55)
    txtCost.Enabled = False
    CostCell.Controls.Add(txtCost)
    myTableRow.Cells.Add(CostCell)

    Dim SaleCell As New TableCell
    Dim txtSale As TextBox = New TextBox
    txtSale.ID = "txtSale" & i
    txtSale.Width = Unit.Pixel(55)
    txtSale.Enabled = False
    SaleCell.Controls.Add(txtSale)
    myTableRow.Cells.Add(SaleCell)

    Dim ProfitCell As New TableCell
    Dim txtProfit As TextBox = New TextBox
    txtProfit.ID = "txtProfit" & i
    txtProfit.Width = Unit.Pixel(60)
    txtProfit.Enabled = False
    ProfitCell.Controls.Add(txtProfit)
    myTableRow.Cells.Add(ProfitCell)

    Dim deleteCell As New TableCell
    Dim chkAccDelete As CheckBox = New CheckBox
    chkAccDelete.ID = "chkAccDelete" & i
    chkAccDelete.Width = Unit.Pixel(60)
    deleteCell.Controls.Add(chkAccDelete)
    myTableRow.Cells.Add(deleteCell)

    accomodation.Rows.Add(myTableRow)
    Return accomodation

End Function

Function accomodationDetails(ByVal i As Int32) As Table
    accomodationDetails = New Table()
    accomodationDetails.BorderColor = Color.Black
    accomodationDetails.BorderStyle = BorderStyle.Solid
    '------1st Row
    Dim tableRow1 As New TableRow
    Dim mycell1 As New TableCell
    mycell1.Text = "Description"
    mycell1.BackColor = Color.Gray
    'Dim tb As TextBox = New TextBox
    tableRow1.Cells.Add(mycell1)

    Dim mycell2 As New TableCell
    mycell2.Text = "Single"
    mycell2.BackColor = Color.Gray
    tableRow1.Cells.Add(mycell2)

    Dim mycell3 As New TableCell
    mycell3.Text = "Double"
    mycell3.BackColor = Color.Gray
    tableRow1.Cells.Add(mycell3)

    Dim mycell4 As New TableCell
    mycell4.Text = "Triple"
    mycell4.BackColor = Color.Gray
    tableRow1.Cells.Add(mycell4)

    Dim mycell5 As New TableCell
    mycell5.Text = "Child"
    mycell5.BackColor = Color.Gray
    tableRow1.Cells.Add(mycell5)

    Dim mycell6 As New TableCell
    mycell6.Text = "Teen"
    mycell6.BackColor = Color.Gray
    tableRow1.Cells.Add(mycell6)

    Dim mycell7 As New TableCell
    mycell7.Text = "Child(OR)"
    mycell7.BackColor = Color.Gray
    tableRow1.Cells.Add(mycell7)
    accomodationDetails.Rows.Add(tableRow1)

    '------2nd Row
    Dim tableRow2 As New TableRow
    Dim mycell1R2 As New TableCell
    mycell1R2.Text = "Cost"
    tableRow2.Cells.Add(mycell1R2)

    Dim mycell2R2 As New TableCell
    Dim txtSingle As TextBox = New TextBox
    txtSingle.ID = "txtSingle" & i
    txtSingle.Width = Unit.Pixel(60)
    mycell2R2.Controls.Add(txtSingle)
    tableRow2.Cells.Add(mycell2R2)

    Dim mycell3R2 As New TableCell
    Dim txtDouble As TextBox = New TextBox
    txtDouble.ID = "txtDouble" & i
    txtDouble.Width = Unit.Pixel(60)
    mycell3R2.Controls.Add(txtDouble)
    tableRow2.Cells.Add(mycell3R2)
    accomodationDetails.Rows.Add(tableRow2)

    Dim mycell4R2 As New TableCell
    Dim txtTriple As TextBox = New TextBox
    txtTriple.ID = "txtTriple" & i
    txtTriple.Width = Unit.Pixel(60)
    mycell4R2.Controls.Add(txtTriple)
    tableRow2.Cells.Add(mycell4R2)
    accomodationDetails.Rows.Add(tableRow2)

    Dim mycell5R2 As New TableCell
    Dim txtChild As TextBox = New TextBox
    txtChild.ID = "txtChild" & i
    txtChild.Width = Unit.Pixel(60)
    mycell5R2.Controls.Add(txtChild)
    tableRow2.Cells.Add(mycell5R2)
    accomodationDetails.Rows.Add(tableRow2)

    Dim mycell6R2 As New TableCell
    Dim txtTeen As TextBox = New TextBox
    txtTeen.ID = "txtTeen" & i
    txtTeen.Width = Unit.Pixel(60)
    mycell6R2.Controls.Add(txtTeen)
    tableRow2.Cells.Add(mycell6R2)

    Dim mycell7R2 As New TableCell
    Dim txtChildOR As TextBox = New TextBox
    txtChildOR.ID = "txtChildOR" & i
    txtChildOR.Width = Unit.Pixel(60)
    mycell7R2.Controls.Add(txtChildOR)
    tableRow2.Cells.Add(mycell7R2)
    accomodationDetails.Rows.Add(tableRow2)

    '------3rd Row
    Dim tableRow3 As New TableRow
    Dim mycell1R3 As New TableCell
    mycell1R3.Text = "Sales"
    tableRow3.Cells.Add(mycell1R3)

    Dim mycell2R3 As New TableCell
    Dim txtSingleSale As TextBox = New TextBox
    txtSingleSale.ID = "txtSingleSale" & i
    txtSingleSale.Width = Unit.Pixel(60)
    mycell2R3.Controls.Add(txtSingleSale)
    tableRow3.Cells.Add(mycell2R3)

    Dim mycell3R3 As New TableCell
    Dim txtDoubleSale As TextBox = New TextBox
    txtDoubleSale.ID = "txtDoubleSale" & i
    txtDoubleSale.Width = Unit.Pixel(60)
    mycell3R3.Controls.Add(txtDoubleSale)
    tableRow3.Cells.Add(mycell3R3)

    Dim mycell4R3 As New TableCell
    Dim txtTripleSale As TextBox = New TextBox
    txtTripleSale.ID = "txtTripleSale" & i
    txtTripleSale.Width = Unit.Pixel(60)
    mycell4R3.Controls.Add(txtTripleSale)
    tableRow3.Cells.Add(mycell4R3)

    Dim mycell5R3 As New TableCell
    Dim txtChildSale As TextBox = New TextBox
    txtChildSale.ID = "txtChildSale" & i
    txtChildSale.Width = Unit.Pixel(60)
    mycell5R3.Controls.Add(txtChildSale)
    tableRow3.Cells.Add(mycell5R3)

    Dim mycell6R3 As New TableCell
    Dim txtTeenSale As TextBox = New TextBox
    txtTeenSale.ID = "txtTeenSale" & i
    txtTeenSale.Width = Unit.Pixel(60)
    mycell6R3.Controls.Add(txtTeenSale)
    tableRow3.Cells.Add(mycell6R3)

    Dim mycell7R3 As New TableCell
    Dim txtChildORSale As TextBox = New TextBox
    txtChildORSale.ID = "txtChildORSale" & i
    txtChildORSale.Width = Unit.Pixel(60)
    mycell7R3.Controls.Add(txtChildORSale)
    tableRow3.Cells.Add(mycell7R3)
    accomodationDetails.Rows.Add(tableRow3)

    '-----4th Row
    Dim tableRow4 As New TableRow
    Dim mycell1R4 As New TableCell
    mycell1R4.Text = "Voucher remarks"
    tableRow4.Cells.Add(mycell1R4)

    Dim mycell2R4 As New TableCell
    mycell2R4.ColumnSpan = "6"
    Dim txtVoucherRemarks As TextBox = New TextBox
    txtVoucherRemarks.ID = "txtVoucherRemarks" & i
    txtVoucherRemarks.Width = Unit.Pixel(420)
    txtVoucherRemarks.TextMode = TextBoxMode.MultiLine
    mycell2R4.Controls.Add(txtVoucherRemarks)
    tableRow4.Cells.Add(mycell2R4)
    accomodationDetails.Rows.Add(tableRow4)

    '------5th Row
    Dim tableRow5 As New TableRow
    Dim mycell1R5 As New TableCell
    Dim chkPark As New CheckBox
    chkPark.ID = "chkPark" & i
    chkPark.Text = "Include park fees."
    mycell1R5.Text = ""
    mycell1R5.Controls.Add(chkPark)
    tableRow5.Cells.Add(mycell1R5)

    Dim mycell2R5 As New TableCell
    mycell2R5.ColumnSpan = "2"
    Dim btnVoucherProcess As Button = New Button
    btnVoucherProcess.ID = "btnVoucherProcess" & i
    btnVoucherProcess.Width = Unit.Pixel(120)
    btnVoucherProcess.Text = "Book"
    mycell2R5.Controls.Add(btnVoucherProcess)
    tableRow5.Cells.Add(mycell2R5)

    Dim mycell3R5 As New TableCell
    mycell3R5.ColumnSpan = "2"
    Dim btnVoucherCancel As Button = New Button
    btnVoucherCancel.ID = "btnVoucherCancel" & i
    btnVoucherCancel.Width = Unit.Pixel(120)
    btnVoucherCancel.Text = "Cancel voucher"
    mycell3R5.Controls.Add(btnVoucherCancel)
    tableRow5.Cells.Add(mycell3R5)

    Dim mycell4R5 As New TableCell
    mycell4R5.ColumnSpan = "2"
    Dim btnVoucherPrint As Button = New Button
    btnVoucherPrint.ID = "btnVoucherPrint" & i
    btnVoucherPrint.Width = Unit.Pixel(120)
    btnVoucherPrint.Text = "Print voucher"
    mycell4R5.Controls.Add(btnVoucherPrint)
    tableRow5.Cells.Add(mycell4R5)

    accomodationDetails.Rows.Add(tableRow5)

    Return accomodationDetails
End Function

以下代码由手风琴外的按钮触发

       Protected Sub btnSaveAccomodation_Click(sender As Object, e As System.EventArgs) Handles btnSaveAccomodation.Click
    For i As Int32 = 0 To acAccomodation.Panes.Count - 1
        'For Each i As In Me.cdTransport.Items
        ' Dim btnVoucherProcess As Button = CType(acAccomodation.FindControl("btnVoucherProcess" & i), Button) 'New Button("btnVoucherProcess" & i)
        Dim controlName As String = "btnVoucherProcess" & i
        Dim btnVoucherProcess As Button = CType(acAccomodation.Panes(i).ContentContainer.FindControl("btnVoucherProcess" & i), Button)
        btnVoucherProcess.Text = "Saved " & i
        'Dim cboDay As DropDownList = CType(i.FindControl("cboDay"), DropDownList)
    Next

End Sub
4

1 回答 1

0

如果您正在动态呈现控件,然后您试图通过事件处理程序访问动态控件,例如按钮的 Click 方法实现,您将一无所获。

为了始终找到动态生成的控件,您应该始终在 Page_Load 事件中创建它们。总是。因此,尝试访问您的动态控件的事件处理程序会找到它们,我无法解释为什么,但是这个动态控件将包含用户在客户端输入的值。

于 2012-06-17T23:46:45.960 回答