如何从动态按钮获取价值

时间:2015-05-18 08:30:33

标签: vb.net

我使用此

在Vb.Net代码中创建了一个动态按钮
 Private Sub getreport()
        Using conn As New SqlConnection()
            conn.ConnectionString = ConfigurationManager _
                .ConnectionStrings("Connectionstring").ConnectionString()
            Using cmd As New SqlCommand()
                'cmd.CommandText = "SELECT [UserName],[ReportName] FROM [BizView_Dev2].[dbo].[ReportPrivilege]  where username =  '" & Session("UAuthen") & "'"
                cmd.CommandText = "SELECT * from report"
                cmd.Connection = conn
                conn.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        Dim BtnRepName = New Button
                        BtnRepName.Style.Add("text-align", "center")
                        BtnRepName.Style.Add("verticle-align", "middle")
                        BtnRepName.Style.Add("display", "table-cell")
                        BtnRepName.Style.Add("margin", "17px")
                        BtnRepName.Style.Add("border-radius", "0%")
                        BtnRepName.CssClass = "btn  btn-lg btn-scb "
                        BtnRepName.Width = 300
                        BtnRepName.Height = 100
                        AddHandler BtnRepName.Click, AddressOf BtnRepName_Click
                        BtnRepName.Text = sdr("rep_name").ToString()
                        PlaceHolder1.Controls.Add(BtnRepName)

                    End While
                End Using
                conn.Close()
            End Using
        End Using
    End Sub

但我不知道如何从每个按钮获取值当我点击按钮将从每个按钮获得一个值,如果我点击一个生成为“TReport”的按钮然后它会在文本框中显示Treport这样

Private Sub BtnRepName_Click(ByVal sender As Object, ByVal e As EventArgs)
  txtrep.text = BtnRepName.text 
End Sub

1 个答案:

答案 0 :(得分:1)

您可以投射"发件人"按钮然后获取文本的值。

Dim senderButton = DirectCast(sender, Button)
textrep.text = senderButton.Text