Bind data source to asp:DropDownList : DropDownList « ADO.net Database « ASP.Net

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » ADO.net Database » DropDownList 
Bind data source to asp:DropDownList

<%@ Page Language="VB" %>
<script runat="server">
        Function AllPublishers() As System.Data.SqlClient.SqlDataReader
            Dim connectionString As String = "server='(local)\NetSDK'; trusted_connection=true; Database='pubs'"
            Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
    
            Dim queryString As String = "SELECT [publishers].* FROM [publishers]"
            Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
            sqlConnection.Open
            Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
            Return dataReader
        End Function
        
    Sub Page_Load(sender As Object, e As EventArgs)
      Page.DataBind()
    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:CheckBoxList id="CheckBoxList1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:CheckBoxList>
        </p>
        <p>
            <asp:RadioButtonList id="RadioButtonList1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:RadioButtonList>
        </p>
        <p>
            <asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:DropDownList>
        </p>
        <p>
            <asp:ListBox id="ListBox1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:ListBox>
        </p>
        <p>
            <asp:Repeater id="Repeater1" runat="server" DataSource="<%# AllPublishers() %>">
                <HeaderTemplate>
                    <b>Publisher List:</b>
                    <br />
                </HeaderTemplate>
                <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "pub_name"%> (ID: <%# DataBinder.Eval(Container.DataItem, "pub_id"%><font size="-1"><i> <%# DataBinder.Eval(Container.DataItem, "city"%>, <%# DataBinder.Eval(Container.DataItem, "state"%>, <%# DataBinder.Eval(Container.DataItem, "country"%> 
                    <br />
                    </i></font> 
                </ItemTemplate>
            </asp:Repeater>
        </p>
        <p>
            <asp:DataList id="DataList1" runat="server" DataSource="<%# AllPublishers() %>">
                <ItemTemplate>
                    <p>
                        ID: 
                        <asp:Label id="Label6" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "pub_id"%>'></asp:Label>
                        &nbsp;Name: 
                        <asp:Label id="Label7" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "pub_name"%>'></asp:Label>
                    </p>
                    <p>
                        Address: 
                        <asp:Label id="Label8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "city"%>'></asp:Label>
                        
                        <asp:Label id="Label9" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "state"%>'></asp:Label>
                        
                        <asp:Label id="Label10" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "country"%>'></asp:Label>
                    </p>
                </ItemTemplate>
                <HeaderTemplate>
                    <asp:Label id="Label1" runat="server" Font-Names="Tahoma" Font-Italic="True">List of publishers:</asp:Label>
                    <hr />
                </HeaderTemplate>
                <FooterTemplate>
                    <hr />
                </FooterTemplate>
                <SeparatorTemplate>
                    <hr />
                    <span style="WIDTH: 100%; HEIGHT: 100%"
                    <div contenteditable="true" style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px; PADDING-BOTTOM: 8px; WIDTH: 100%; PADDING-TOP: 8px; HEIGHT: 100%"></div>
                    </span> 
                </SeparatorTemplate>
            </asp:DataList>
        </p>
    </form>
</body>
</html>

           
       
Related examples in the same category
1. Bind data from OleDbConnection to asp:dropdownlist
2. Binding Database Data to a DropDownList Control
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.