如何将DropDownList与数据库中的数据绑定 [英] How bind DropDownList with data from database

查看:66
本文介绍了如何将DropDownList与数据库中的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试填写< asp:DropDownList>来自数据库的数据,但我似乎无法让它工作,因为没有显示。我不确定我是否完全忽略了这一点,或者我只是没有正确地将下拉列表与数据联系起来。





To事情清楚我在一个页面上有一个div,它显示一个包含一些数据的表和带有两个按钮的下拉列表。填充表格的数据来自一个隐秘的页面,看不见我只用它来创建表格然后在页面上

Hi Everyone,

I am trying to fill an <asp:DropDownList> with data from a database but i cant seem to get it to work as nothing shows. I am not sure if i am completely missing the point or whther i am just not linking the dropdown with the data correctly.


To make things clear i have a div on one page which shows a table with some data and the dropdownlist with two buttons. The data to fill the table comes from a stealth page that is not seen i only use it to create the table and then on the page the

所在我用JavaScript来调用表格数据。(对不起,如果这不是有意义)



i需要使用相同的方法进行下拉菜单,甚至是填充下拉列表的不同方法,但我似乎无法做到这一点。



我的代码..



这是我的div位于我的搜索页面

is located i use JavaScript to call the table data.(sorry if this doesn't make sense)

i need to use the same method for the dropdown or even a diferent way to populate the dropdown but i cant seem to do this.

My Code..

yhis is my div located on my search page
<div id="dUserSystems" runat="server" >
        <u>Systems user has access to</u><br />
            <div id="dUserData"></div>
        <br /><br />
    <div id="ddlSysData" runat="server" >
         <u>Add new system to users account</u><br />

        <asp:DropDownList ID="sysDDL" runat="server"></asp:DropDownList>

    </div>
        <br /><br />
        <input type="button" id="butAdd" value="Add" runat="server" style="font-family: Verdana,Tahoma,Arial; float: left;"/>&nbsp;&nbsp;
        <input type="button" onclick="butClose()" value="Close" />
</div>





以下是我的服务器端代码,为来自不同页面的表格





below is my server side code creating the data for the table from a different page

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

        'Add session check here

        If Request.QueryString("userid") <> "" Then

            Dim dsResults As New DataSet
            Dim dt As DataTableReader

            dsResults = GetUserName.systemData(Request.QueryString("userid").ToString)
            dt = dsResults.CreateDataReader

            Dim dv As New HtmlGenericControl("div")
            dv.ID = "sysData"

            Dim tb As New Table
            Dim tr As New TableRow
            Dim tc As New TableCell
            Dim lb As New Label
            Dim iCount As Integer = 1

            If dt.HasRows = True Then

                tr = New TableRow
                tr.CssClass = "tabHeader"
                tc = New TableCell
                tc.CssClass = "tabCell"
                lb = New Label

                lb.Text = "System Name"
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)

                tr.CssClass = "tabHeader"
                tc = New TableCell
                tc.CssClass = "tabCell"
                lb = New Label

                lb.Text = "Date Added"
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)


                tr.CssClass = "tabHeader"
                tc = New TableCell
                tc.CssClass = "tabCell"
                lb = New Label

                lb.Text = "Added By"
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)

                Do While dt.Read

                    tr = New TableRow

                    If iCount = 1 Then
                        tr.CssClass = "rowStyle3"
                        iCount = 0
                    Else
                        tr.CssClass = "rowStyle4"
                        iCount = 1
                    End If

                    'tr = New TableRow
                    tc = New TableCell
                    tc.CssClass = "rowCell2"
                    lb = New Label

                    lb.Text = dt.Item("description").ToString()
                    tc.Controls.Add(lb)
                    tr.Controls.Add(tc)
                    tb.Controls.Add(tr)

                    'tr = New TableRow
                    tc = New TableCell
                    tc.CssClass = "rowCell2"
                    lb = New Label


                    lb.Text = dt.Item("date_added").ToString()
                    tc.Controls.Add(lb)
                    tr.Controls.Add(tc)
                    tb.Controls.Add(tr)

                    'tr = New TableRow
                    tc = New TableCell
                    tc.CssClass = "rowCell2"
                    lb = New Label

                    lb.Text = dt.Item("added_by").ToString()
                    tc.Controls.Add(lb)
                    tr.Controls.Add(tc)
                    tb.Controls.Add(tr)
                Loop
                dt.Close()
                dsResults.Dispose()


                dv.Controls.Add(tb)
                sysData.Controls.Add(dv)
            Else
                Response.Write("No Systems Located for User")
            End If


        End If





我尝试用下面的数据创建一个表格用于下拉但我没有运气。



如果我有困惑或者您需要更多信息请告诉我。



谢谢



I have tried creating a table with the data as above for the drop down but i have had no luck.

If i have confused or you need more info please let me know.

Thank You





**更新信息**



我列出了一些可能有助于解决我的问题的更多信息。



1.我在搜索页面上有一个按钮,按下时会将数据显示为在搜索页面上创建的样式化弹出窗口。



2.数据在div中是从另一个页面(fetchData.aspx)调用,我正在创建如上所示的表格。



3.在(fetchData.aspx)上页面我有



**Updated Info**

I am listing some further info that may help with my question.

1. I have a button on a search page that when pressed shows the data as a styled popup that is created on the search page.

2. The data that is within the div is called from another page(fetchData.aspx) that i am creating the table on as shown above.

3. On the (fetchData.aspx) page i have

<asp:PlaceHolder ID="sysData" runat="server"></asp:PlaceHolder> 





搜索页面上的完整



Full

..



on search page..

<div id="dUserSystems" runat="server" >
            <u>Systems user has access to</u><br />
                <div id="dUserData"></div>
            <br /><br />
        <div id="ddlSysData" runat="server" >
             <u>Add new system to users account</u><br />

            <asp:DropDownList ID="sysDDL" runat="server"></asp:DropDownList>

        </div>
            <br /><br />
            <input type="button" id="butAdd" value="Add" runat="server"/>&nbsp;
            <input type="button" onclick="butClose()" value="Close" />
    </div>







推荐答案

这应该设置你的数据源。看看它是否适合自动绑定。然后你可以玩属性。



This should set your datasource. See if it goes well with autobinding. Then you can play with properties.

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

        'Add session check here

        If Request.QueryString("userid") <> "" Then

            Dim dsResults As New DataSet
            Dim dt As DataTableReader

            dsResults = GetUserName.systemData(Request.QueryString("userid").ToString)
        sysDll.DataSource = dsResults.Tables(0) 
End Sub







注意:下拉列表只有两个值DataTextField(即description)和DataValueField(即ID) 。您要做的可能是调用DataList,DataGridView或asp:Repeater,您可以在其中绑定多个列。



你目前正在做什么是错过了重点。您正在使用列表控件,然后创建表格以在其中显示。如果你想要这种格式,你根本不需要使用列表控件,只需将你的表附加到任何div。



列表控件将生成他们自己的网格而不需要你必须做手工工作。您可以设置列,分配数据源并让框架/控件工作。然后你调整样式直到你满意为止。




NOTE: Drop down list has only two values DataTextField (i.e. description) and DataValueField (i.e. id) . What you're trying to do is probably calling for DataList, DataGridView or asp:Repeater where you can bind multiple columns.

What you're currently doing IS missing the point. You're using list control and then create the table to show inside it. If you want that format, you don't have to use list control at all, just append your table to any div.

List controls will generate their own grids without you having to do manual work. You set the columns, assign datasource and let the framework / control work. Then you tweak styles until you're satisfied.


这篇关于如何将DropDownList与数据库中的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆