在 vb.net 的数据读取器中添加数据集

发布于 2024-08-23 11:42:49 字数 1283 浏览 1 评论 0原文

我希望我正确地提出了问题标题。让我解释一下我的问题 - 我正在通过 vb.net 代码(htmltablecell、htmltablerow..)制作一个表,没有这个表填充 SQL 查询并且工作完美。但在这个表的一个表单元格中,我需要添加一个下拉列表,该列表需要完全不同的查询,并且应在每一行上单独运行。所以代码如下

Sql = "..." rd = ExecuteReader(SqlCnn, Sql)

        Dim newcounter As Integer = 0
        While rd.Read()
            newcounter += 1
            Dim tr As New HtmlTableRow
            Dim td As New HtmlTableCell
            Dim chkbox As New CheckBox
            Dim id As String = rd("id") & ""
            If id.Length = 0 Then id = "new" & newcounter
            id &= "_" & rd("col1")
            chkbox.Style.Add("width", "20%")
            chkbox.ID = "new_id_" & id
            chkbox.Text = rd("col1")

            tr.Style.Add("width", "100%")
            td.Style.Add("width", "10%")
            td.Style.Add("text-align", "left")
            td.Controls.Add(chkbox)
            tr.Cells.Add(td)

            td = New HtmlTableCell
            td.Style.Add("width", "30%")
            Dim ddl As New dropdownlist
            ddl.Style.Add("width", "80%")
            ddl.ID = "a1_" & id
            --???????
            td.Controls.Add(ddl)
            tr.Cells.Add(td)

???在代码中,下拉列表每次都应使用自己的数据读取器以及 while 循环和查询进行填充。我怎样才能做到这一点?

i hope i have asked the question title correctly. let me explain my issue -
i am making a table through vb.net code (htmltablecell, htmltablerow..) no this table populates with an sql query and works perfectly. but inside this table in one tablecell, i need to add a dropdownlist which shall require a completely differernt query and shall run on its own on each row. so the code is as follows

Sql = "..."
rd = ExecuteReader(SqlCnn, Sql)

        Dim newcounter As Integer = 0
        While rd.Read()
            newcounter += 1
            Dim tr As New HtmlTableRow
            Dim td As New HtmlTableCell
            Dim chkbox As New CheckBox
            Dim id As String = rd("id") & ""
            If id.Length = 0 Then id = "new" & newcounter
            id &= "_" & rd("col1")
            chkbox.Style.Add("width", "20%")
            chkbox.ID = "new_id_" & id
            chkbox.Text = rd("col1")

            tr.Style.Add("width", "100%")
            td.Style.Add("width", "10%")
            td.Style.Add("text-align", "left")
            td.Controls.Add(chkbox)
            tr.Cells.Add(td)

            td = New HtmlTableCell
            td.Style.Add("width", "30%")
            Dim ddl As New dropdownlist
            ddl.Style.Add("width", "80%")
            ddl.ID = "a1_" & id
            --???????
            td.Controls.Add(ddl)
            tr.Cells.Add(td)

the ??? in the code is where the dropdownlist shall get populated each time with its own datareader and while loop and query. how can i achieve this?

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

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

发布评论

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

评论(1

染柒℉ 2024-08-30 11:42:49

我会通过两个查询来完成此操作; 1 获取 ddl 数据,1 获取实际记录。在实际结果之前获取ddl数据(ddlDataReader),创建ddl对象并将其添加到每一行。

像这样的东西应该可以工作:(我不做VB.Net,所以你可能需要修复一些东西)

    Dim ddl As New dropdownlist
        ddl.Style.Add("width", "80%")
        ddl.ID = "a1_" & id
    While ddlDataReader.Read()
        ---add items to ddl
    end


    Dim newcounter As Integer = 0
    While rd.Read()
        newcounter += 1
        Dim tr As New HtmlTableRow
        Dim td As New HtmlTableCell
        Dim chkbox As New CheckBox
        Dim id As String = rd("id") & ""
        If id.Length = 0 Then id = "new" & newcounter
        id &= "_" & rd("col1")
        chkbox.Style.Add("width", "20%")
        chkbox.ID = "new_id_" & id
        chkbox.Text = rd("col1")

        tr.Style.Add("width", "100%")
        td.Style.Add("width", "10%")
        td.Style.Add("text-align", "left")
        td.Controls.Add(chkbox)
        tr.Cells.Add(td)

        td = New HtmlTableCell
        td.Style.Add("width", "30%")
        td.Controls.Add(ddl)
        tr.Cells.Add(td)

另一种方法是将ddl数据加载到数据表中,这样你就可以多次循环它,然后创建创建每一行时具有唯一 ID 的 ddl。

I would do this with 2 queries; 1 to get the ddl data and 1 to get the actual records. Get the ddl data (ddlDataReader) before the actual results, create the ddl object and add it to each row.

Something like this should work: (I don't do VB.Net, so you may need to fix things)

    Dim ddl As New dropdownlist
        ddl.Style.Add("width", "80%")
        ddl.ID = "a1_" & id
    While ddlDataReader.Read()
        ---add items to ddl
    end


    Dim newcounter As Integer = 0
    While rd.Read()
        newcounter += 1
        Dim tr As New HtmlTableRow
        Dim td As New HtmlTableCell
        Dim chkbox As New CheckBox
        Dim id As String = rd("id") & ""
        If id.Length = 0 Then id = "new" & newcounter
        id &= "_" & rd("col1")
        chkbox.Style.Add("width", "20%")
        chkbox.ID = "new_id_" & id
        chkbox.Text = rd("col1")

        tr.Style.Add("width", "100%")
        td.Style.Add("width", "10%")
        td.Style.Add("text-align", "left")
        td.Controls.Add(chkbox)
        tr.Cells.Add(td)

        td = New HtmlTableCell
        td.Style.Add("width", "30%")
        td.Controls.Add(ddl)
        tr.Cells.Add(td)

Another approach would be to load the ddl data into a datatable, so that you can loop through it multiple times, and then create the ddl with a unique Id when each row is being created.

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