【问题标题】:Dynamically redirect to another page with PostBackUrl ASP.NET使用 PostBackUrl ASP.NET 动态重定向到另一个页面
【发布时间】:2017-09-16 17:55:23
【问题描述】:

我有一个动态创建的表。此表只有一个字段 - 类别。从每个类别中,我创建一个按钮。我希望每个按钮都重定向到特定页面。每个页面 aspx 都有一个类别名称 .aspx。 有没有办法动态地做一次?我用Selected Case创建了这个,但这不适合我,因为我每次都会创建和删除类别

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

【问题讨论】:

    标签: sql asp.net vb.net dynamic web-applications


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2011-08-22
      • 2014-09-01
      • 1970-01-01
      相关资源
      最近更新 更多