从动态按钮调用sub后面的代码

时间:2014-02-24 19:56:48

标签: asp.net vb.net .net-4.0

我正在使用以下代码生成动态按钮:

Dim btn As New Button()
btn.Attributes.Add("runat", "server")
btn.Attributes.Add("OnClick", "btnCreate_Click()")

我有按钮显示并试图调用以下子:

Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Redirect("https:\\www.google.com", False)
End Sub

即使我调试代码永远不会进入sub。

如何从动态生成按钮调用代码?

编辑:

如果这将有用,那么按钮就位于asp:table

tableCellButton.Controls.Add(btn)
...
table_row.Cells.Add(tableCellButton)
...
inbox_table.Rows.Add(table_row)

完整代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    DisplayTable()
End Sub

Private Sub DisplayTable()

    If member_id Is Nothing Then
        Exit Sub
    Else

        Dim dbContext As New *Hidden*
        Dim inbox = From p In dbContext.Inboxes Where p.RecordId = member_id Select p
        Dim inboxList As IEnumerable(Of *Hidden*) = inbox.ToList()

        If inboxList.Count < 1 Then
            Exit Sub
        End If

        For Each i In inboxList

            Dim table_row As TableRow = New TableRow()

            Dim tableCellInboxId As TableCell = New TableCell()
            Dim tableCellRecordId As TableCell = New TableCell()
            Dim tableCellMemberId As TableCell = New TableCell()
            Dim tableCellImportance As TableCell = New TableCell()
            Dim tableCellReceivedFrom As TableCell = New TableCell()
            Dim tableCellReceivedSubject As TableCell = New TableCell()
            Dim tableCellReceivedDateTime As TableCell = New TableCell()
            Dim tableCellReceivedBody As TableCell = New TableCell()
            Dim tableCellButton As TableCell = New TableCell()

            ' Dynamic Button
            Dim btn As New Button
            btn.Text = "Click Me"
            btn.ID = "ClickMeID"

            AddHandler btn.Click, AddressOf btnCreate_Click

            tableCellInboxId.Text = i.InboxId.ToString
            tableCellRecordId.Text = i.RecordId.ToString
            tableCellMemberId.Text = i.MemberId.ToString
            tableCellImportance.Text = i.Importance.ToString
            tableCellReceivedFrom.Text = i.ReceivedFrom.ToString
            tableCellReceivedSubject.Text = i.ReceivedSubject.ToString
            tableCellReceivedDateTime.Text = i.ReceivedDateTime.ToString
            tableCellReceivedBody.Text = i.ReceivedBody.ToString
            tableCellButton.Controls.Add(btn)

            table_row.Cells.Add(tableCellInboxId)
            table_row.Cells.Add(tableCellRecordId)
            table_row.Cells.Add(tableCellMemberId)
            table_row.Cells.Add(tableCellImportance)
            table_row.Cells.Add(tableCellReceivedFrom)
            table_row.Cells.Add(tableCellReceivedSubject)
            table_row.Cells.Add(tableCellReceivedDateTime)
            table_row.Cells.Add(tableCellReceivedBody)
            table_row.Cells.Add(tableCellButton)

            inbox_table.Rows.Add(table_row)

        Next

    End If

End Sub

Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = DirectCast(sender, Button)
    If btn.ID = "ClickMeID" Then
        'Response.Redirect(blablabla)
    End If
End Sub

2 个答案:

答案 0 :(得分:2)

AddHandler btn.Click, AddressOf btnCreate_Click应该连接事件处理程序服务器端。我认为你不需要你设置的两个属性。您还需要在页面的表单中添加按钮。

以下是使用VS2013中使用母版页的默认WebForms模板的示例。

Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init

    ' Note: This also seems to work from Page_Load

    Dim button = New Button()
    button.Text = "Click Me"
    ' MainContent is the ContentPlaceHolderID of the content control in the master page
    Me.Master.FindControl("MainContent").Controls.Add(button)
    AddHandler button.Click, AddressOf MyButton_Clicked

End Sub

Protected Sub MyButton_Clicked(sender As Object, a As EventArgs)
    Debug.Print("Clicked!")
End Sub

答案 1 :(得分:0)

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
 Dim btn As New Button
 btn.Text = "Click Me"
 btn.ID = "btnClickMe"
 AddHandler btn.Click, AddressOf btnCreate_Click
End Sub

Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
 Dim btn As Button = DirectCast(sender, Button)
 If btn.ID = "btnClickMe" Then
   Response.Redirect(blablabla)
 End If
End Sub

我举一个简单的例子,你可以在新子行的第一行之后做你想做的所有事情。 就像有人在这里写的那样,你不需要那些属性。 您可以创建多个按钮,并在btnCreate_Click子管理它们,在您的情况下,重定向到页面。 顺便说一句。最好的方法是在Page_Init中创建这些按钮。 希望我帮助你。

其他例子:

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
 For x = 1 To 3
  Dim btn As New Button
  btn.Text = "Button #" + x.ToString()
  btn.ID = "btn" + x.ToString()
  AddHandler btn.Click, AddressOf btnCreate_Click
 Next
End Sub

Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
 Dim btn As Button = DirectCast(sender, Button)
 If btn.ID = "btn1" Then
   Response.Redirect("http://www.google.com")
 ElseIf btn.ID = "btn2" Then
   Response.Redirect("http://www.yahoo.com")
 Else 'this is btn3
   Response.Redirect("http://www.facebook.com")
 End If
End Sub
相关问题