从代码隐藏动态创建和访问RadioButtonLists

发布于 2024-07-12 23:59:53 字数 1728 浏览 9 评论 0原文

我的问题是:我有一个精简的 default.aspx 页面。 除了元素之外,几乎所有功能都必须位于 aspx.vb 的代码隐藏中。

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

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

代码示例如下:

aspx 文件:

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


aspx.vb file (code-behind):

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

抱歉,如果这又长又令人困惑,但由于我什至还无法正确形成我的问题,所以我尝试包含尽可能多的信息。 基本上我只需要一个示例来说明如何以及使用哪些方法来使所有这些工作正常进行。

更新:

每个人都一直告诉我,从一开始就使用表格是一个错误,但我发现有些地方声称您无法像使用表格一样轻松地自定义数据网格外观。 我想我会从头开始整个事情。

问题是,用户界面应该在图形上尽可能令人愉悦,通过表格,我可以做各种巧妙的事情,例如根据内部信息为单元格着色等。

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

My problem is: I have a stripped down default.aspx page. Aside from a element, almost ALL the functionality has to be in the code-behind in aspx.vb.

The program retreives information from a database and compares this to another list of tables, number of both lists can vary.

So I need to "bind" a dynamic number of RadioButtonLists to the asp:table element controls, and I need to create a dynamic number of ListItems to each RadioButtonList created. Later, I need to be able to access the selected value of each in order to decide future functions in the database.

An example of the code is like this:

aspx file:

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


aspx.vb file (code-behind):

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

Sorry if this is long and confusing, but as I can't even form my questions right yet, I tried to include as much information as possible. Basically I just need an example of how and which methods to use to get all that working.

Update:

Everyone keeps telling me that going for tables to begin with was a mistake, but some place I found claimed that you can't customize datagrid looks as easily as you can with tables. I guess I'll start the whole thing from scratch then.

Thing is that the UI should be as graphically pleasing as possible, with the tables I can do all sorts of neat things such as colouring the cells according to the information inside etc.

Anyway, thanks but not what I was looking for but I'll try to remake the thing with datagrid / gridview and see what happens. Might take a few days before I learn enough of them to use them and get back here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-07-19 23:59:53

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

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

Rather than a table, you'll have better luck with a true server control like gridview or datagrid, or repeater. Then you can just wire up the datasource property on your control.

For all dynamic controls, remember that you have to recreate them every time you do a postback. This may sound odd, but the trick is the every request to the server, including postbacks, uses a different instance of your page class. When that request/postback finishes the page instance used for that request is destroyed. The only things left active are the session and the cache.

长安忆 2024-07-19 23:59:53

这是我的 .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

This is my .aspx code.

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

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

This is my code behind file code.
I have added dynamic drop-down list.
On save button click, I am retrieving selected value.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文