ASP.Net/ADO.net Database/DropDownList — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:51, 26 мая 2010

Bind data from OleDbConnection to asp:dropdownlist

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBCommand As OleDbDataAdapter
        Dim DSPageData as New DataSet
        "DBConn = New OleDbConnection( _
        "    "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        "    & "DATA SOURCE=" _
        "    & Server.MapPath("EmployeeDatabase.mdb;"))
        "DBConn = New OleDbConnection( _
        "    "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        "    & "DATA SOURCE=" _
        "    & "c:\inetpub\wwwroot\EmployeeDatabase.mdb;")
        DBConn = New OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & MapPath("EmployeeDatabase.mdb;"))
        DBCommand = New OleDbDataAdapter _
            ("Select LastName & ", " & FirstName " _
            & "as EmpName, ID " _
            & "From Employee " _
            & "Order By LastName, FirstName", DBConn)
        DBCommand.Fill(DSPageData, _
            "Employee")
        ddlEmps.DataSource = _
            DSPageData.Tables("Employee").DefaultView
        ddlEmps.DataBind()
    End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Connecting to an Access Database</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:dropdownlist
    id="ddlEmps"
    datatextfield="EmpName" 
    datavaluefield="ID"
    runat="server"
/>
</form>
</BODY>
</HTML>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


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.rumandBehavior.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>



Binding Database Data to a DropDownList Control

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBCommand As OleDbDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
        DBCommand = New OleDbDataAdapter _
            ("Select ID, FirstName " _
            & "From Employee " _
            & "Order By FirstName", DBConn)
        DBCommand.Fill(DSPageData, _
            "Employee")
        ddlEmployees.DataSource = _
            DSPageData.Tables("Employee").DefaultView
        ddlEmployees.DataBind()
    End If
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    lblDataSelected.Text = "Selected Text: " _
        & ddlEmployees.SelectedItem.Text _
        & "<BR>Selected Value: " _
        & ddlEmployees.SelectedItem.Value _
        & "<BR>Selected Index: " _
        & ddlEmployees.SelectedIndex       
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Binding Database Data to a DropDownList Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:label 
    id="lblDataSelected" 
    runat="server"
/>
<BR><BR>
<asp:dropdownlist
    id="ddlEmployees"
    datatextfield="FirstName" 
    datavaluefield="ID"
    runat="server"
/>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    onclick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>