前台代码:Default.aspx
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml" >
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
<head runat="server">
 <title>无标题页</title>
    <title>无标题页</title>
 </head>
</head>
 <body>
<body>
 <form id="form1" runat="server">
    <form id="form1" runat="server">
 <div>
    <div>
 <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox> 
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox> 
 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=">" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=">" />
 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=">>" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=">>" />
 <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox></div>
        <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox></div>
 </form>
    </form>
 </body>
</body>
 </html>
</html>
 后台代码:
后台代码:
 using System;
using System;
 using System.Data;
using System.Data;
 using System.Configuration;
using System.Configuration;
 using System.Web;
using System.Web;
 using System.Web.Security;
using System.Web.Security;
 using System.Web.UI;
using System.Web.UI;
 using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;
 using System.Data.SqlClient;
using System.Data.SqlClient;

 public partial class _Default : System.Web.UI.Page
public partial class _Default : System.Web.UI.Page 
 {
{
 protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e)
 {
    {
 if (!IsPostBack)
        if (!IsPostBack)
 {
        {
 this.ListBindData();
            this.ListBindData();
 }
        }
 }
    }
 public void ListBindData()
    public void ListBindData()
 {
    {
 
        
 string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
        string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
 SqlConnection con = new SqlConnection(connstr);
        SqlConnection con = new SqlConnection(connstr);
 if (con.State.Equals(ConnectionState.Closed))
        if (con.State.Equals(ConnectionState.Closed))
 {
        {
 con.Open();
            con.Open();
 }
        }
 SqlCommand cmd = new SqlCommand("select * from guestbook",con);
        SqlCommand cmd = new SqlCommand("select * from guestbook",con);
 SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
 //这是第一种方法了
        //这是第一种方法了
 //while (sdr.Read())
        //while (sdr.Read())
 //{
        //{
 //    ListBox1.Items.Add(new ListItem(sdr["title"].ToString(),sdr["id"].ToString()));
        //    ListBox1.Items.Add(new ListItem(sdr["title"].ToString(),sdr["id"].ToString()));
 //}
        //}
 ListBox1.DataSource = sdr;
        ListBox1.DataSource = sdr;
 ListBox1.DataTextField = "title";
        ListBox1.DataTextField = "title";
 ListBox1.DataValueField = "id";
        ListBox1.DataValueField = "id";
 ListBox1.DataBind();
        ListBox1.DataBind();
 }
    }
 protected void Button1_Click(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
 {
    {
 foreach (ListItem item in ListBox1.Items)
        foreach (ListItem item in ListBox1.Items)
 {
        {
 ListBox2.Items.Add(item);
            ListBox2.Items.Add(item);
 
           
 }
        }
 ListBox1.Items.Clear();
        ListBox1.Items.Clear();
 }
    }
 protected void Button2_Click(object sender, EventArgs e)
    protected void Button2_Click(object sender, EventArgs e)
 {
    {
 ListBox2.Items.Add(ListBox1.SelectedItem);
        ListBox2.Items.Add(ListBox1.SelectedItem);
 ListBox1.SelectedIndex = -1;
        ListBox1.SelectedIndex = -1;
 ListBox2.SelectedIndex = -1;
        ListBox2.SelectedIndex = -1;
 }
    }
 }
}
 
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<head runat="server"> <title>无标题页</title>
    <title>无标题页</title> </head>
</head> <body>
<body> <form id="form1" runat="server">
    <form id="form1" runat="server"> <div>
    <div> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox> 
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>  <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=">" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=">" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=">>" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=">>" /> <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox></div>
        <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox></div> </form>
    </form> </body>
</body> </html>
</html>
 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls; using System.Data.SqlClient;
using System.Data.SqlClient;
 public partial class _Default : System.Web.UI.Page
public partial class _Default : System.Web.UI.Page  {
{ protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e) {
    { if (!IsPostBack)
        if (!IsPostBack) {
        { this.ListBindData();
            this.ListBindData(); }
        } }
    } public void ListBindData()
    public void ListBindData() {
    { 
         string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
        string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString(); SqlConnection con = new SqlConnection(connstr);
        SqlConnection con = new SqlConnection(connstr); if (con.State.Equals(ConnectionState.Closed))
        if (con.State.Equals(ConnectionState.Closed)) {
        { con.Open();
            con.Open(); }
        } SqlCommand cmd = new SqlCommand("select * from guestbook",con);
        SqlCommand cmd = new SqlCommand("select * from guestbook",con); SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //这是第一种方法了
        //这是第一种方法了 //while (sdr.Read())
        //while (sdr.Read()) //{
        //{ //    ListBox1.Items.Add(new ListItem(sdr["title"].ToString(),sdr["id"].ToString()));
        //    ListBox1.Items.Add(new ListItem(sdr["title"].ToString(),sdr["id"].ToString())); //}
        //} ListBox1.DataSource = sdr;
        ListBox1.DataSource = sdr; ListBox1.DataTextField = "title";
        ListBox1.DataTextField = "title"; ListBox1.DataValueField = "id";
        ListBox1.DataValueField = "id"; ListBox1.DataBind();
        ListBox1.DataBind(); }
    } protected void Button1_Click(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e) {
    { foreach (ListItem item in ListBox1.Items)
        foreach (ListItem item in ListBox1.Items) {
        { ListBox2.Items.Add(item);
            ListBox2.Items.Add(item); 
            }
        } ListBox1.Items.Clear();
        ListBox1.Items.Clear(); }
    } protected void Button2_Click(object sender, EventArgs e)
    protected void Button2_Click(object sender, EventArgs e) {
    { ListBox2.Items.Add(ListBox1.SelectedItem);
        ListBox2.Items.Add(ListBox1.SelectedItem); ListBox1.SelectedIndex = -1;
        ListBox1.SelectedIndex = -1; ListBox2.SelectedIndex = -1;
        ListBox2.SelectedIndex = -1; }
    } }
}
 
                     
                    
                 
                    
                 


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号