Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.Net中动态控件的回发事件问题_Asp.net_Dynamic_Postback - Fatal编程技术网

ASP.Net中动态控件的回发事件问题

ASP.Net中动态控件的回发事件问题,asp.net,dynamic,postback,Asp.net,Dynamic,Postback,我目前正在处理一个页面,该页面有一个用于构建动态表的用户控件。控件最初加载在Page_Init事件上,每当引发更改动态控件的事件时,表都会重新加载。这里的问题是,如果控件在两个负载之间更改,则控件中的事件不会触发。例如,表中最初有两行。在回发期间,一个项目被添加到表中,现在有四行(此表一次添加两行)。每行有一个或两个按钮。加载页面并将其发送回浏览器时,如果用户单击任何按钮,则会发生回发,但不会触发事件。我做错了什么?如何判断是什么控制/事件导致回发?下面是页面和用户控件的代码 支付。aspx:

我目前正在处理一个页面,该页面有一个用于构建动态表的用户控件。控件最初加载在Page_Init事件上,每当引发更改动态控件的事件时,表都会重新加载。这里的问题是,如果控件在两个负载之间更改,则控件中的事件不会触发。例如,表中最初有两行。在回发期间,一个项目被添加到表中,现在有四行(此表一次添加两行)。每行有一个或两个按钮。加载页面并将其发送回浏览器时,如果用户单击任何按钮,则会发生回发,但不会触发事件。我做错了什么?如何判断是什么控制/事件导致回发?下面是页面和用户控件的代码

支付。aspx:

Partial Public Class Payments
Inherits BasePage

Private foodMaster As Food
Private _check As BusinessLayer.CustomerCheck

Private Sub btnAddCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCheck.Click
    ' do nothing. the modal window is tied to the button via the modal window in the designer        
End Sub

Private Sub btnCalendar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCalendar.Click
    calCheckDate.Visible = Not calCheckDate.Visible
    modCheck.Show()
End Sub

Private Sub btnCheckSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckSave.Click
    Try
        If IsNothing(_check) Then _check = New BusinessLayer.CustomerCheck
        If Me.CurrentCheck > 0 Then _check.CheckId = Me.CurrentCheck
        _check.CheckNumber = txtCheckNumber.Text
        _check.CheckDate = CDate(txtCheckDate.Text)
        _check.CheckAmount = CDbl(txtCheckAmount.Text)
        _check.DepositId = Me.CurrentDeposit

        _check.Save()

        LoadControls()
        ' reset the current check to not get confused after an edit
        Me.CurrentCheck = 0
        SetupNewCheck()
    Catch ex As Exception
        lblMessage.Text = "Could not save check."
        lblMessage.Visible = True
        modCheck.Show()
    End Try
End Sub

Private Sub calCheckDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calCheckDate.SelectionChanged
    txtCheckDate.Text = calCheckDate.SelectedDate.ToShortDateString()
    calCheckDate.Visible = False
    modCheck.Show()
End Sub

Private Sub cvFutureDate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvFutureDate.ServerValidate
    Try
        Dim depositDate As DateTime = DateTime.Parse(txtCheckDate.Text)
        Dim futureDate As DateTime = Now.AddDays(1)
        Dim tomorrow As New DateTime(futureDate.Year, futureDate.Month, futureDate.Day)

        args.IsValid = CBool(depositDate < tomorrow)
    Catch
        args.IsValid = False
    End Try
End Sub

Private Sub cvInvalidAmount_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvInvalidAmount.ServerValidate
    Try
        Double.Parse(txtCheckAmount.Text)
    Catch
        args.IsValid = False
    End Try
End Sub

Private Sub cvInvalidDate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvInvalidDate.ServerValidate
    Try
        DateTime.Parse(txtCheckDate.Text)
    Catch
        args.IsValid = False
    End Try
End Sub

Private Sub DepositEditing()
    foodMaster.Deposit.Load(Me.CurrentDeposit)
    foodMaster.ShowDepositWindow()
End Sub

Private Sub DepositSaved()
    dihHeader.Deposit.Load(Me.CurrentDeposit)
    dihHeader.Reload()
End Sub

Private Sub Payments_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    LoadControls()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    foodMaster = DirectCast(Me.Master, Food)
    AddHandler foodMaster.SaveClicked, AddressOf DepositSaved
    AddHandler foodMaster.EditButtonClicked, AddressOf DepositEditing

    If IsPostBack = False Then
        Me.CurrentCheck = 0
        SetupNewCheck()
    End If
End Sub

Private Sub pcPayments_ApplyFundsClicked(ByVal CheckId As Integer) Handles pcPayments.ApplyFundsClicked

End Sub

Private Sub pcPayments_DeleteClicked(ByVal CheckId As Integer) Handles pcPayments.DeleteClicked
    Try
        If Me.CurrentCheck = CheckId Then Me.CurrentCheck = 0
        _check = New BusinessLayer.CustomerCheck
        _check.CheckId = CheckId
        _check.DeleteAllPayments()
        _check.Delete()

        LoadControls()
    Catch

    End Try
End Sub

Private Sub pcPayments_EditClicked(ByVal CheckId As Integer) Handles pcPayments.EditClicked
    Me.CurrentCheck = CheckId
    _check = New BusinessLayer.CustomerCheck(CheckId)

    txtCheckAmount.Text = _check.CheckAmount.ToString("0.00")
    txtCheckDate.Text = _check.CheckDate.ToShortDateString
    calCheckDate.SelectedDate = _check.CheckDate
    txtCheckNumber.Text = _check.CheckNumber

    modCheck.Show()
End Sub

Private Sub LoadControls()
    Dim checks As New BusinessLayer.CustomerCheckCollection()
    checks.LoadByDeposit(Me.CurrentDeposit)
    pcPayments.Checks = checks
    pcPayments.Reload()

    dihHeader.Deposit.Load(Me.CurrentDeposit)
    dihHeader.TotalCheckAmount = pcPayments.TotalCheckAmount
    dihHeader.TotalAppliedAmount = pcPayments.TotalAmountApplied
    dihHeader.Reload()
End Sub

Private Sub SetupNewCheck()
    _check = Nothing
    txtCheckDate.Text = Now.ToShortDateString()
    calCheckDate.SelectedDate = Now

    txtCheckAmount.Text = String.Empty
    txtCheckNumber.Text = String.Empty
End Sub

End Class
部分公共类付款
继承BasePage
私人食品管理员
Private\u检查为BusinessLayer.CustomerCheck
私有子btnAddCheck_Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理btnAddCheck。单击
“什么也不做。模式窗口通过设计器中的模式窗口绑定到按钮
端接头
私有子btnCalendar\u单击(ByVal sender作为对象,ByVal e作为System.Web.UI.ImageClickEventArgs)处理btnCalendar。单击
calCheckDate.Visible=不是calCheckDate.Visible
modCheck.Show()
端接头
私有子btnCheckSave_Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理btnCheckSave。单击
尝试
如果没有(_check),则_check=New BusinessLayer.CustomerCheck
如果Me.CurrentCheck>0,则_check.CheckId=Me.CurrentCheck
_check.CheckNumber=txtCheckNumber.Text
_check.CheckDate=CDate(txtCheckDate.Text)
_check.CheckAmount=CDbl(txtCheckAmount.Text)
_check.DepositId=Me.CurrentDeposit
_check.Save()
LoadControls()
'在编辑后重置当前检查以避免混淆
Me.CurrentCheck=0
SetupNewCheck()
特例
lblMessage.Text=“无法保存支票。”
lblMessage.Visible=True
modCheck.Show()
结束尝试
端接头
私有子calCheckDate_SelectionChanged(ByVal sender作为对象,ByVal e作为System.EventArgs)处理calCheckDate.SelectionChanged
txtCheckDate.Text=calCheckDate.SelectedDate.ToSortDateString()
calCheckDate.Visible=False
modCheck.Show()
端接头
私有子cvFutureDate_ServerValidate(ByVal源作为对象,ByVal参数作为System.Web.UI.WebControl.ServerValidateEventArgs)处理cvFutureDate.ServerValidate
尝试
Dim depositDate As DateTime=DateTime.Parse(txtCheckDate.Text)
Dim futureDate As DateTime=Now.AddDays(1)
将明天暗显为新日期时间(futureDate.Year,futureDate.Month,futureDate.Day)
args.IsValid=CBool(存款日期<明天)
抓住
args.IsValid=False
结束尝试
端接头
私有子cvInvalidAmount_ServerValidate(ByVal源作为对象,ByVal args作为System.Web.UI.WebControls.ServerValidateEventArgs)处理cvInvalidAmount.ServerValidate
尝试
Double.Parse(txtCheckAmount.Text)
抓住
args.IsValid=False
结束尝试
端接头
私有子cvInvalidDate_ServerValidate(ByVal源作为对象,ByVal参数作为System.Web.UI.WebControl.ServerValidateEventArgs)处理cvInvalidDate.ServerValidate
尝试
Parse(txtCheckDate.Text)
抓住
args.IsValid=False
结束尝试
端接头
私人转存户
foodMaster.Deposit.Load(Me.CurrentDeposit)
foodMaster.showDepositorWindow()
端接头
私人转存户
dihHeader.存款.加载(Me.活期存款)
dihHeader.Reload()
端接头
Private Sub Payments_Init(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Init
LoadControls()
端接头
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
foodMaster=DirectCast(Me.Master,食品)
AddHandler foodMaster.SaveClicked,地址已保存
AddHandler foodMaster.EditButtonClicked,存款编辑地址
如果IsPostBack=False,则
Me.CurrentCheck=0
SetupNewCheck()
如果结束
端接头
私有子pcPayments\u ApplyFundsClicked(ByVal CheckId为整数)处理pcPayments.ApplyFundsClicked
端接头
私有子pcPayments\u DeleteClicked(ByVal CheckId为整数)处理pcPayments.DeleteClicked
尝试
如果Me.CurrentCheck=CheckId,则Me.CurrentCheck=0
_检查=新建BusinessLayer.CustomerCheck
_check.CheckId=CheckId
_选中。删除所有付款()
_选中。删除()
LoadControls()
抓住
结束尝试
端接头
私有子pcPayments_EditClicked(ByVal CheckId为整数)处理pcPayments.EditClicked
Me.CurrentCheck=CheckId
_check=新的BusinessLayer.CustomerCheck(检查ID)
txtCheckAmount.Text=\u check.CheckAmount.ToString(“0.00”)
txtCheckDate.Text=\u check.CheckDate.ToSortDateString
calCheckDate.SelectedDate=\u check.CheckDate
txtCheckNumber.Text=\u check.CheckNumber
modCheck.Show()
端接头
专用子加载控件()
作为新的BusinessLayer.CustomerCheckCollection()进行Dim检查
支票。按存款(Me.活期存款)
pcPayments.Checks=支票
pcPayments.Reload()
dihHeader.存款.加载(Me.活期存款)
dihHeader.TotalCheckAmount=pcPayments.TotalCheckAmount
dihHeader.TotalAppliedAmount=pcPayments.totalamount未应用
dihHeader.Reload()
端接头
私有子设置newcheck()
_支票=无
txtCheckDate.Text=Now.ToShortDateString()
calCheckDate.SelectedDate=现在
txtCheckAmount.Text=String.Empty
txtCheckNumber.Text=String.Empty
端接头
末级
PaymentsControl.ascx

Public Partial Class PaymentsControl
Inherits System.Web.UI.UserControl

Private _checks As BusinessLayer.CustomerCheckCollection
Private _applied As Double

Public Event ApplyFundsClicked(ByVal CheckId As Integer)
Public Event DeleteClicked(ByVal CheckId As Integer)
Public Event EditClicked(ByVal CheckId As Integer)

Public Sub New()
    _checks = New BusinessLayer.CustomerCheckCollection
    _applied = 0
End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'If IsPostBack = False Then
    '    BindChecks()
    'End If
End Sub

Private Sub ApplyButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
    RaiseEvent ApplyFundsClicked(DirectCast(sender, LinkButton).CommandArgument)
End Sub

Private Sub DeleteButtonClicked(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    RaiseEvent DeleteClicked(DirectCast(sender, ImageButton).CommandArgument)
End Sub

Private Sub EditButtonClicked(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    RaiseEvent EditClicked(DirectCast(sender, ImageButton).CommandArgument)
End Sub

Private Sub BindChecks()
    tblChecks.Rows.Clear()
    tblChecks.Rows.Add(BuildTableHeader())

    _applied = 0

    For i As Int16 = 0 To _checks.Count - 1
        _checks(i).LoadAllPayments()
        _applied += _checks(i).TotalAmountApplied

        tblChecks.Rows.Add(BuildCheckRow(_checks(i)))
        tblChecks.Rows.Add(BuildInvoiceRow(_checks(i)))
    Next

    If tblChecks.Rows.Count = 1 Then tblChecks.Visible = False
End Sub

Private Function BuildCheckRow(ByVal Check As BusinessLayer.CustomerCheck) As TableRow
    Dim checkNumberCell As New TableCell()
    Dim checkDateCell As New TableCell()
    Dim checkAmountCell As New TableCell()
    Dim totalAppliedCell As New TableCell()

    checkNumberCell.Text = Check.CheckNumber
    checkDateCell.Text = Check.CheckDate.ToShortDateString()
    checkAmountCell.Text = Check.CheckAmount.ToString("C")
    totalAppliedCell.Text = Check.TotalAmountApplied.ToString("C")
    If Check.TotalAmountApplied <> Check.CheckAmount Then totalAppliedCell.ForeColor = Drawing.Color.Red

    Dim myRow As New TableRow
    myRow.Cells.Add(BuildCheckControlCell(Check.CheckId))
    myRow.Cells.Add(checkNumberCell)
    myRow.Cells.Add(checkDateCell)
    myRow.Cells.Add(checkAmountCell)
    myRow.Cells.Add(totalAppliedCell)

    Return myRow
End Function

Private Function BuildCheckControlCell(ByVal CheckId As Integer) As TableCell
    Dim editButton As New ImageButton()
    editButton.CommandArgument = CheckId
    editButton.CausesValidation = False
    editButton.AlternateText = "Edit"
    editButton.ImageUrl = "~/images/icons/bullet_edit.png"
    AddHandler editButton.Click, AddressOf EditButtonClicked

    Dim deleteButton As New ImageButton
    deleteButton.CommandArgument = CheckId
    deleteButton.CausesValidation = False
    deleteButton.AlternateText = "Delete"
    deleteButton.ImageUrl = "~/images/icons/bullet_cross.png"
    deleteButton.Attributes.Add("onclick", "return confirmDelete()")
    AddHandler deleteButton.Click, AddressOf DeleteButtonClicked

    Dim blankSpace As New Literal()
    blankSpace.Text = "&nbsp;"

    Dim myCell As New TableCell
    myCell.Controls.Add(editButton)
    myCell.Controls.Add(blankSpace)
    myCell.Controls.Add(deleteButton)

    Return myCell
End Function

Private Function BuildInvoiceRow(ByVal Check As BusinessLayer.CustomerCheck) As TableRow
    Dim invoiceDetailCell As New TableCell
    invoiceDetailCell.ColumnSpan = 4
    invoiceDetailCell.Controls.Add(BuildInvoiceDetailTable(Check.Payments))

    Dim myRow As New TableRow
    myRow.Cells.Add(BuildInvoiceControlCell(Check.CheckId))
    myRow.Cells.Add(invoiceDetailCell)

    Return myRow
End Function

Private Function BuildInvoiceControlCell(ByVal CheckId As Integer) As TableCell
    Dim text As New Literal
    text.Text = "Invoices for check:<br />"

    Dim applyButton As New LinkButton
    applyButton.CommandArgument = CheckId
    applyButton.CausesValidation = False
    applyButton.Text = "Apply Funds"
    AddHandler applyButton.Click, AddressOf ApplyButtonClicked

    Dim myCell As New TableCell
    myCell.Controls.Add(text)
    myCell.Controls.Add(applyButton)

    Return myCell
End Function

Private Function BuildInvoiceDetailTable(ByVal Payments As BusinessLayer.PaymentTransactionCollection) As Table
    Dim myTable As New Table
    myTable.CssClass = "tableSub"
    myTable.CellPadding = "0"
    myTable.CellSpacing = "0"
    myTable.BorderWidth = "0"
    myTable.Rows.Add(BuildInvoiceDetailHeader())

    For i As Integer = 0 To Payments.Count - 1
        myTable.Rows.Add(BuildPaymentRow(Payments(i)))
    Next

    If myTable.Rows.Count = 1 Then myTable.Visible = False

    Return myTable
End Function

Private Function BuildInvoiceDetailHeader() As TableRow
    Dim customerCell As New TableHeaderCell
    Dim invoiceCell As New TableHeaderCell
    Dim dueCell As New TableHeaderCell
    Dim paymentCell As New TableHeaderCell

    customerCell.Text = "Customer"
    invoiceCell.Text = "Invoice number"
    dueCell.Text = "Amount due"
    paymentCell.Text = "Payment amount"

    Dim myRow As New TableRow
    myRow.Cells.Add(customerCell)
    myRow.Cells.Add(invoiceCell)
    myRow.Cells.Add(dueCell)
    myRow.Cells.Add(paymentCell)

    Return myRow
End Function

Private Function BuildPaymentRow(ByVal Payment As BusinessLayer.PaymentTransaction) As TableRow
    Dim customerCell As New TableCell
    Dim invoiceCell As New TableCell
    Dim amountDueCell As New TableCell
    Dim paymentCell As New TableCell

    'Payment.Customer.Load()
    customerCell.Text = Payment.Customer.NumberAndName
    invoiceCell.Text = Payment.Invoice.InvoiceNumber
    amountDueCell.Text = Payment.Invoice.AmountDue.ToString("C")
    paymentCell.Text = Payment.PaymentAmount.ToString("C")

    Dim myRow As New TableRow
    myRow.Cells.Add(customerCell)
    myRow.Cells.Add(invoiceCell)
    myRow.Cells.Add(amountDueCell)
    myRow.Cells.Add(paymentCell)

    Return myRow
End Function

Private Function BuildTableHeader() As TableRow
    Dim blankCell As New TableHeaderCell()
    Dim checkNumberCell As New TableHeaderCell()
    Dim checkDateCell As New TableHeaderCell()
    Dim checkAmountCell As New TableHeaderCell()
    Dim totalUnappliedCell As New TableHeaderCell()

    checkNumberCell.Text = "Check number"
    checkDateCell.Text = "Check date"
    checkAmountCell.Text = "Check amount"
    totalUnappliedCell.Text = "Total unapplied"

    Dim myRow As New TableRow
    myRow.Cells.Add(blankCell)
    myRow.Cells.Add(checkNumberCell)
    myRow.Cells.Add(checkDateCell)
    myRow.Cells.Add(checkAmountCell)
    myRow.Cells.Add(totalUnappliedCell)

    Return myRow
End Function

Public Sub Reload()
    BindChecks()
End Sub

Public Property Checks() As BusinessLayer.CustomerCheckCollection
    Get
        Return _checks
    End Get
    Set(ByVal value As BusinessLayer.CustomerCheckCollection)
        _checks = value
    End Set
End Property

Public ReadOnly Property TotalCheckAmount() As Double
    Get
        Return _checks.TotalCheckAmount
    End Get
End Property

Public ReadOnly Property TotalAmountApplied() As Double
    Get
        Return _applied
    End Get
End Property

End Class
公共部分类付款控件
继承System.Web.UI.UserControl
Private\u检查为BusinessLayer.Cust