Emma W.
Emma W.

Reputation: 215

Dynamically redirect to another page with PostBackUrl ASP.NET

I have a dynamically created table. This table has only one field - category. From each category I create a button. And I want that every button have redirect to a specific page. Each page aspx has a category name .aspx. Is there a way to do it once dynamically? I created this with Selected Case, but this does not suit me, because I will create and delete categories every time

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

Upvotes: 1

Views: 424

Answers (1)

Zsmaster
Zsmaster

Reputation: 1559

XAML:

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

C#:
I used a list, but you can change it.

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

Upvotes: 2

Related Questions