ASP.NET Click事件未在自定义Panel控件内的按钮中触发

时间:2014-08-20 11:33:51

标签: asp.net vb.net event-handling

我试图用VB学习ASP.NET。

我已使用以下代码创建了自定义的Panel控件。

    Public Class RowPanel
    Inherits Panel
    Private WithEvents pnlMain As New Panel
    Private btnRack() As Button

    Public Sub New(Optional ByRef NoOfRacks As Integer = 8)
        createPanel(NoOfRacks)
    End Sub

    Public Sub createPanel(ByVal NoOfRacks As Integer)

        With pnlMain
            .Height = 600
            .Width = 200
            .BackColor = Drawing.Color.BurlyWood
            .BorderStyle = WebControls.BorderStyle.Dashed
        End With

        ReDim btnRack(NoOfRacks - 1)
        For i = 1 To NoOfRacks

            btnRack(i - 1) = New Button

            With btnRack(i - 1)
                .Width = pnlMain.Width.Value - 20
                .ID = "Rack" & Guid.NewGuid().ToString("N")
                .Text = "Rack" & i
                'AddHandler .Click, AddressOf rackbutton_Click
            End With

        Next

        Dim bt As Button

        For Each bt In btnRack
            AddHandler bt.Click, AddressOf rackbutton_Click
        Next

        For b As Integer = 0 To btnRack.GetUpperBound(0)
            Dim brk As New LiteralControl("</br>")

            pnlMain.Controls.Add(btnRack(b))
            pnlMain.HorizontalAlign = WebControls.HorizontalAlign.Center
            pnlMain.Controls.Add(brk)
        Next
        Me.Controls.Add(pnlMain)
    End Sub

    Protected Sub rackbutton_Click(sender As Object, e As EventArgs)

        Dim clickedbtn As Button
        clickedbtn = CType(sender, Button)
        MsgBox(clickedbtn.ID.ToString)
    End Sub

End Class

我的问题是当我点击面板中的按钮时,rackbutton_Click事件没有触发。

主要网络代码:

Public Class WebForm1

Inherits System.Web.UI.Page

Private dynamicPnlIDs As New List(Of String)
Const _MaxRacks As Integer = 10
Dim rck As Integer = 1

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Session("dynamicBtnIDs") IsNot Nothing Then

        dynamicPnlIDs = DirectCast(Session("dynamicBtnIDs"), List(Of String))

        For Each pnlID As String In dynamicPnlIDs
            Dim rk As Integer
            rk = Right(pnlID, pnlID.Length - (1 + pnlID.IndexOf(":"c)))
            Dim pnl As New RowPanel(rk)
            Dim tblCell As New TableCell
            pnl.ID = Right(pnlID, pnlID.IndexOf(":"c) - 1).ToString()
            tblCell.Controls.Add(pnl)
            Table1.Rows(0).Controls.Add(tblCell)
        Next

    Else
        dynamicPnlIDs = New List(Of String)()
    End If
End Sub

Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender

    Session("dynamicBtnIDs") = dynamicPnlIDs

End Sub

Protected Sub addRow_Click(sender As Object, e As EventArgs) Handles addRow.Click

    rck = TextBox1.Text

    If Not (rck > 0 And rck <= _MaxRacks) Then
        MsgBox("Invalid Entry!" & vbCrLf & "Please enter a value between 1 &" & _MaxRacks & ".", vbOKOnly, "Error")
        TextBox1.BackColor = Drawing.Color.PaleVioletRed
        Exit Sub
    Else
        TextBox1.BackColor = System.Drawing.ColorTranslator.FromHtml("#CDCDCD")
    End If

    Dim pnl As New RowPanel(rck)
    Dim tblCell As New TableCell

    pnl.ID = "pnl" & Guid.NewGuid().ToString("N")
    tblCell.Controls.Add(pnl)
    Table1.Rows(0).Controls.Add(tblCell)
    dynamicPnlIDs.Add(pnl.ID & ":" & rck.ToString())

End Sub
End Class

请帮帮我。 (这是我的第一篇文章,欢迎任何建议)

1 个答案:

答案 0 :(得分:0)

删除或注释掉以下行,它应该开始工作。

'.ID = "Rack" & Guid.NewGuid().ToString("N")

如果您愿意,可以将其替换为有意义的内容。

e.g。

.ID = pnlMain.UniqueID & "_Rack" & i.ToString