0

我的问题是:我有一个精简的default.aspx页面。除了一个元素,几乎所有的功能都必须在aspx.vb.

该程序从数据库中检索信息并将其与另一个表列表进行比较,两个列表的数量可能会有所不同。

所以我需要将动态数量的 RadioButtonLists “绑定”到asp:table元素控件,并且我需要为创建的每个 RadioButtonList 创建动态数量的 ListItems。稍后,我需要能够访问每个选定的值,以便决定数据库中的未来功能。

代码示例如下:

.aspx 文件:

<asp:table ID="table1" runat="server">


aspx.vb 文件(代码隐藏):

Sub createHtmlTables()

    For i = 0 To productIndex.Count - 1

        ''//create a RadioButtonList for each i
        Dim row As New TableRow
        Dim cell As New TableCell

        For k = 0 To productTypeAmountIndex.Count - 1

            ''//create a ListItem(radiobutton)
            ''//for each k and include it in the RadioButtonList

            ''//assign a value (for example name) of the product as
            ''//the ListItems ID to retreive it later

        Next

        ''//add the RadioButtonList to cell.controls etc
        Table1.Rows.Add(row)

    Next
End Sub

Sub addToDb()
    For i = 0 To productIndex.Count - 1
        ''//get the RadioButtonList for each i
        ''//and return the value of the selected radiobutton
        ''//within the list to a variable

    Next

End Sub

抱歉,如果这很长而且令人困惑,但由于我什至还不能正确地提出我的问题,所以我试图包含尽可能多的信息。基本上我只需要一个例子来说明如何以及使用哪些方法来使所有这些工作。

更新:

每个人都一直告诉我,从表格开始是一个错误,但是我发现的某个地方声称您不能像使用表格那样轻松地自定义数据网格的外观。我想我会从头开始整件事。

问题是 UI 应该尽可能图形化,使用表格我可以做各种整洁的事情,例如根据里面的信息为单元格着色等。

无论如何,谢谢,但不是我想要的,但我会尝试用 datagrid / gridview 重新制作这个东西,看看会发生什么。可能需要几天的时间,我才能学到足够多的东西来使用它们并回到这里。

4

2 回答 2

0

与表格相比,使用真正的服务器控件(如 gridview 或 datagrid 或中继器)会更好。然后你可以在你的控件上连接数据源属性。

对于所有动态控件,请记住每次执行回发时都必须重新创建它们。这听起来可能很奇怪,但诀窍是对服务器的每个请求,包括回发,都使用页面类的不同实例。当该请求/回发完成时,用于该请求的页面实例被销毁。唯一保持活动状态的是会话和缓存。

于 2009-02-04T15:20:37.023 回答
0

这是我的 .aspx 代码。

<asp:Table ID="Questions" runat="server">

</asp:Table>
<asp:Button ID="SaveButton" runat="server" Text="Save" />

这是我在文件代码后面的代码。我添加了动态下拉列表。单击保存按钮时,我正在检索选定的值。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim NewRow As New TableRow
    Dim NewCell As New TableCell
    Dim rblOptions As New RadioButtonList
    rblOptions.ID = "Option1"

    rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("1", "1"))
    rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("2", "2"))
    rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("3", "3"))

    NewCell.Controls.Add(rblOptions)
    NewRow.Cells.Add(NewCell)
    'Questions is a table
    Questions.Rows.Add(NewRow)

End Sub

Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
    If Page.IsValid Then
        Dim rbl As RadioButtonList = DirectCast(Questions.FindControl("Option1"), RadioButtonList)

        If rbl.SelectedValue.ToString = "ExpectedValue" Then

        End If
    End If
End Sub
于 2015-05-18T09:00:23.533 回答