根据从DB返回的数据,从后面的代码创建链接按钮

时间:2014-12-08 18:53:52

标签: c# asp.net vb.net

我需要帮助动态创建链接按钮,我正在从数据库中填充数据,并根据我要在我的页面上创建的数据,链接按钮可以有人请帮我这个... < / p>

*(我需要删除下面列表中的重复项)

我的VB.NET代码:

 Dim RegionName As String = Nothing
        Dim RegionID As String = Nothing
        Dim output As String = Nothing

        For Each dr As DataRow In dt.Rows()
            RegionName = dr.Item("RegionName").ToString()
            RegionID = dr.Item("RegionID").ToString()

            '# Creating instance of LinkButton
            Dim lb As New LinkButton()
            lb = New LinkButton()
            lb.Text = RegionName '# LinkButton Text
            lb.ID = RegionID.ToString() '# LinkButton ID's
            lb.CommandArgument = RegionID.ToString() '# LinkButton CommandArgument
            lb.CommandName = RegionName '# LinkButton CommanName
            AddHandler lb.Click, AddressOf lb_Command '# Create Handler for it
            PlaceHolder.Controls.Add(lb)

            Dim lbl As New Label()
            lbl.Text = "<br>"
            PlaceHolder.Controls.Add(lbl)

        Next


Private Sub lb_Command(sender As Object, e As CommandEventArgs)
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    lblDynamicRegion.Text = e.CommandName
    '# Will display the which Linkbutton clicked
    If lnk.Font.Bold = True Then
        lnk.Font.Bold = False
        lnk.ForeColor = System.Drawing.Color.Blue
    Else
        lnk.Font.Bold = True
        lnk.ForeColor = System.Drawing.Color.Green
    End If

End Sub

我要为其创建链接按钮的数据库中的列表:

SouthEast (MD, NC, FL)
MidWest
SouthEast (MD, NC, FL)
SouthEast (MD, NC, FL)
NorthWest
San Diego
Los Angeles
MidWest
SouthEast (MD, NC, FL)
NorthEast (NY, CT)
SouthEast (MD, NC, FL)
Los Angeles
NorthWest
Centro Properties Group
Los Angeles
Los Angeles
MidWest
MidWest
NorthWest
NorthEast (NY, CT)
MidWest
MidWest
MidWest
San Diego
Demo
MidWest
Los Angeles
NorthEast (NY, CT)
NorthWest
Centro Properties Group
San Diego
San Diego
SouthEast (MD, NC, FL)
San Diego
NorthWest
MidWest
Los Angeles
San Diego
San Diego
Los Angeles
Centro Properties Group
NorthWest
Los Angeles
SouthEast (MD, NC, FL)
NorthWest
MidWest
NorthEast (NY, CT)
NorthWest
SouthEast (MD, NC, FL)
MidWest
Los Angeles
NorthEast (NY, CT)
San Diego
Los Angeles
NorthWest
NorthWest
Los Angeles
Centro Properties Group
SouthEast (MD, NC, FL)
SouthEast (MD, NC, FL)

1 个答案:

答案 0 :(得分:0)

如果您的数据已排序,则会更容易。目前,要删除结果,您必须记住&#39;你见过的每一个独特的价值。这假定区域的文本对于重复的项目完全相同。或者,您可以使用RegionID。

此外,当您使用new关键字声明对象时,它会创建对象的实例,因此您不需要稍后实例化该对象。这就是我删除lb = New LinkBut​​ton()语句的原因。

        Dim RegionName As String = Nothing
        Dim RegionID As String = Nothing
        Dim output As String = Nothing
        Dim Regions as New System.Collections.Specialized.StringCollction()

        For Each dr As DataRow In dt.Rows()
            RegionName = dr.Item("RegionName").ToString()
            If Not Regions.Contains(RegionName) Then
               Regions.Add(RegionName)
               RegionID = dr.Item("RegionID").ToString()

               '# Creating instance of LinkButton
               Dim lb As New LinkButton()
               lb.Text = RegionName '# LinkButton Text
               lb.ID = RegionID.ToString() '# LinkButton ID's
               lb.CommandArgument = RegionID.ToString() '# LinkButton CommandArgument
               lb.CommandName = RegionName '# LinkButton CommanName
               AddHandler lb.Click, AddressOf lb_Command '# Create Handler for it
               PlaceHolder.Controls.Add(lb)

               Dim lbl As New Label()
               lbl.Text = "<br>"
               PlaceHolder.Controls.Add(lbl)
           End If

        Next


Private Sub lb_Command(sender As Object, e As CommandEventArgs)
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    lblDynamicRegion.Text = e.CommandName
    '# Will display the which Linkbutton clicked
    If lnk.Font.Bold = True Then
        lnk.Font.Bold = False
        lnk.ForeColor = System.Drawing.Color.Blue
    Else
        lnk.Font.Bold = True
        lnk.ForeColor = System.Drawing.Color.Green
    End If

End Sub