Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
is located i use JavaScript to call the table data.(sorry if this doesn't make sense) and 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
XML
<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

VB
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
Posted
Updated 19-Jan-15 22:56pm
v2

Very basic and simple : Bind Dropdownlist in ASP.NET using jQuery AJAX[^]

Regards..
 
Share this answer
 
If you are using ASP.NET you can use one of server side language in your code behind file and fetch the data from database and then bind it to a control easilly. for some example check below link
DataBinding DropDownList Label and Textbox Controls in ASP.Net[^]
 
Share this answer
 
Comments
Member 11355710 12-Jan-15 9:53am    
Thank you for your help but this seems complicated and i would not know where to start. I was hoping for a simple javascript or jquery function to use client side to pull the data if possible
Thanks7872 12-Jan-15 23:09pm    
I think you didn't read the question properly. This can not b useful to OP in anyway.
DamithSL 12-Jan-15 23:53pm    
Rohan, I know that OP can use ajax and bind the data using service call, but OP need both server side code plus client side code(not simple code) for achive that. I just want to tell him either way OP need to use server side code but he can easily set the datasource and bind it without complex service calls and ajax/javascript binding codes. see the above comments of OP to understand the OPs knowledge on programming.
you still think this as worst ever answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900