ASP.NET Tutorial/Data Binding/ListBox

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

Bind DataSet to ListBox

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DataSetBinding" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="lstUser" runat="server" Height="152px" Width="192px"></asp:ListBox></div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataSetBinding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet dsInternal = new DataSet();
        dsInternal.Tables.Add("Users");
        dsInternal.Tables["Users"].Columns.Add("Name");
        dsInternal.Tables["Users"].Columns.Add("Country");
        DataRow rowNew = dsInternal.Tables["Users"].NewRow();
        rowNew["Name"] = "A";
        rowNew["Country"] = "USA";
        dsInternal.Tables["Users"].Rows.Add(rowNew);
        rowNew = dsInternal.Tables["Users"].NewRow();
        rowNew["Name"] = "B";
        rowNew["Country"] = "Canada";
        dsInternal.Tables["Users"].Rows.Add(rowNew);
        rowNew = dsInternal.Tables["Users"].NewRow();
        rowNew["Name"] = "C";
        rowNew["Country"] = "Japan";
        dsInternal.Tables["Users"].Rows.Add(rowNew);
        lstUser.DataSource = dsInternal.Tables["Users"];
        lstUser.DataTextField = "Name";
        lstUser.DataSource = dsInternal;
        lstUser.DataMember = "Users";
        lstUser.DataTextField = "Name";
        this.DataBind();
    }
}


ListBox Control data binding with asp:SqlDataSource

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblProduct.Text = lstProducts.SelectedItem.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show ListBox</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ListBox
        id="lstProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        DataValueField="Id"
        Rows="8"
        Runat="server" />
    
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        OnClick="btnSubmit_Click"
        Runat="server" />
    
    <hr />
    <asp:Label
        id="lblProduct"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Id, Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </form>
</body>
</html>
            
            
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Selecting multiple list items (set SelectionMode to Multiple)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in lstProducts.Items)
            if (item.Selected)
                lblProduct.Text += "<li>" + item.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Multiple ListBox</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ListBox
        id="lstProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        DataValueField="Id"
        SelectionMode="Multiple"
        Runat="server" />
    
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        OnClick="btnSubmit_Click"
        Runat="server" />
    
    <hr />
    <asp:Label
        id="lblProduct"
        EnableViewState="false"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Id, Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </form>
</body>
</html>
        
            
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Show list items with programmatic binding.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    public class CartItem
    {
        private int _id;
        public string _description;
        public int Id
        {
            get { return _id; }
        }
        public string Description
        {
            get { return _description; }
        }
        public CartItem(int id, string description)
        {
            _id = id;
            _description = description;
        }
    }
    void Page_Load()
    {
        if (!IsPostBack)
        {
            List<CartItem> shoppingCart = new List<CartItem>();
            shoppingCart.Add(new CartItem(1, "A"));
            shoppingCart.Add(new CartItem(2, "B"));
            shoppingCart.Add(new CartItem(3, "C"));
            lstShoppingCart.DataSource = shoppingCart;
            lstShoppingCart.DataBind();
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Programmatic DataBinding</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ListBox
        id="lstShoppingCart"
        DataTextField="Description"
        DataValueField="Id"
        Runat="server" />
    </div>
    </form>
</body>
</html>