ASP.. NET单击事件不触发自定义面板控件内的按钮



我正在努力学习ASP。. NET with VB.

我用下面的代码创建了一个自定义的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

请帮帮我。(这是我的第一个帖子,欢迎任何建议)

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

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

如果你愿意,你可以用一些有意义的东西来代替它。

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

相关内容

  • 没有找到相关文章

最新更新