如何将onclick事件添加到vb.net中以编程方式创建的链接按钮

时间:2016-01-20 11:01:33

标签: asp.net vb.net dynamic-programming asplinkbutton

我希望动态显示我在数据库中使用数据集从我的数据库中获取它们的产品,因为每个商店中的产品数量各不相同,当用户点击表格中的产品时,我将显示所点击的产品屏幕上的产品详情。

问题是我在表的每个单元格中以编程方式添加了一个链接按钮,但是单击事件处理程序无法正常工作。

请问如何添加链接按钮,并动态单击事件处理程序?如何在事件处理程序方法(ShowProductDetails)中获取单击按钮的id?

这是我的代码

products.aspx

 </asp:Content>
   <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table  style=" width: 700px;" align="center">
<tr style="width:100%;">

<td align="center" width="50%" >Stores </td>
<td align="right" width="50%" >
<asp:DropDownList ID="StoresDDL" runat="server" AutoPostBack="True" Width="120px">
</asp:DropDownList>
</td>

</tr>

<tr>
<td >
<asp:table id="ProductsTBL" runat ="server" style="width: 700px;" 
        align="center" BorderStyle="Groove" BorderWidth="3px">
</asp:table>
</td>
</tr>

<tr>
<td>
<asp:Panel  runat ="server" id="ProductDetails_Panel" visible ="false">

<asp:Label ID="selectedProduct" runat ="server" Visible="false" ></asp:Label>
</asp:Panel>
</td>



</tr>
    </table>


   </asp:Content>

Products.aspx.vb

 Imports System.Data



Partial Class Admin_ViewProducts
     Inherits System.Web.UI.Page


Dim con As New Conection
Dim ds_Products As New DataSet 
Dim dr_Products As DataRow
Dim ds_Stores As New DataSet
Dim dr_Stores As DataRow


Dim sB As New LinkButton()


Protected Sub StoresDDL__Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles StoresDDL.Init

    StoresDDL.Items.Clear()
    StoresDDL.Items.Add("Select a store")
    StoresDDL.DataValueField = 0

    Try

            ds_Stores = con.get_data("select storeid , storeName from Store ")

        End If

        Dim x As Integer = 1
        For Each Me.dr_stores In ds_stores.Tables(0).Rows
            If (dr_stores.IsNull(0) = False) Then
                StoresDDL.Items.Add(dr_stores.Item(1))

                StoresDDL.Items(x).Value = dr_stores.Item(0)

                x = x + 1
            End If

        Next

    Catch ex As Exception

    End Try
End Sub

Protected Sub StoresDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StoresDDL.SelectedIndexChanged


    ds_products = con.get_data("select productid ,productName from Product where storeid=" & StoresDDL.SelectedValue ")
    Dim y As Integer = 1

    Dim NOClmns As Integer = 3
    Dim NOCells As Integer = ds_products.Tables(0).Rows.Count
    Dim NORows As Integer = Math.Round(NOCells / 3, MidpointRounding.AwayFromZero)


    ' Current row count
    Dim rowCtr As Integer
    ' Current cell counter.
    Dim cellCnt As Integer

    Dim productCount As Integer = 0
    While productCount <> ds_products.Tables(0).Rows.Count
        For rowCtr = 1 To NORows
            Dim tRow As New TableRow()
            For cellCount = 1 To 4 'For NOCells = 1 To cellCnt

                Dim tCell As New TableCell()
                'Dim s As New HyperLink()
                sB = New LinkButton()
                sB.ID = ds_products.Tables(0).Rows(productCount).Item("productName").ToString
                sB.Text = ds_products.Tables(0).Rows(productCount).Item("productName").ToString

                AddHandler sB.Click, AddressOf Me.ShowProductDetails

                tCell.Controls.Add(sB)
                tRow.Cells.Add(tCell)
                productCount += 1
            Next cellCount   'NOCells
            ' Add new row to table.
            ProductsTBL.Rows.Add(tRow)

        Next rowCtr

    End While
End Sub

Protected Sub ShowProductDetails(ByVal sender As System.Object, ByVal e As System.EventArgs)



    ProductDetails_Panel.Visible = True
    selectedProduct.Visible = True
    selectedProduct.Text = "test"


     End Sub
 End Class

1 个答案:

答案 0 :(得分:0)

为了完成这项工作,您需要使用命令事件作为链接按钮并传递CommandArgument,即产品ID。

所以:

sb.CommandArgument = ds_products.Tables(0).Rows(productCount).Item("productID")
sb.CommandName = "clicked"
sb.OnCommand="LinkButton_Command" 

然后,向后面的代码添加一个事件:

   Sub LinkButton_Command(sender As Object, e As CommandEventArgs) 
      If e.CommandName = "clicked" Then
           Process Request
      End IF
   End Sub

但是,如果是我,我会绑定到DataGrid或Repeater,而不是创建自己的表...