2017-04-20 66 views
1

我有一個動態創建的表。此表只有一個字段類別。從每個類別我創建一個按鈕。我希望每個按鈕都可以重定向到特定的頁面。每個頁面的aspx都有一個類別名稱.aspx。 有沒有辦法一次動態地做到這一點?Selected Case創造了這個,但是這並不適合我,因爲我將創建和刪除類別每動態重定向到PostBackUrl的另一頁ASP.NET

Using myCommand As New SqlCommand("SELECT kategorie FROM Kategorie", myConn) 
      Using myDataReader As SqlDataReader = myCommand.ExecuteReader 

       While myDataReader.Read() 

        Dim tRow As New TableRow() 
        tblKategorie.Rows.Add(tRow) 

        Dim tCell As New TableCell() 
        tRow.Cells.Add(tCell) 

        Dim buttKategorie As New Button 
        buttKategorie.Text = myDataReader("kategorie") 
        buttKategorie.CssClass = "buttKategorie" 

        **'Here I tried to do it > ????** 
        'buttKategorie.PostBackUrl = myDataReader("kategorie.aspx") 

        tCell.Controls.Add(buttKategorie) 

       End While 
      End Using 
End Using 

回答

2

XAML時間:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
    <asp:panel runat="server" id="container"></asp:panel>  
</asp:Content> 

C#:
我用了列表,但你可以改變它。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim source As New List(Of String) 
    source.Add("page1.aspx") 
    source.Add("page2.aspx") 
    For Each tmp As String In source 
     Dim btn As New Button 
     btn.Text = tmp 
     btn.ID = tmp 
     AddHandler btn.Click, AddressOf Me.Button_Click 
     container.Controls.Add(btn) 
    Next 
End Sub 

Private Sub Button_Click(sender As Object, e As EventArgs) 
    Dim btn As Button = CType(sender, Button) 
    Response.Redirect(btn.Text) 
End Sub