<%@ Page language="c#" CodeFile="MyDropDownList.aspx.cs" AutoEventWireup="false" Inherits="MyDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>使用DropDownList控件显示数据库中的内容</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td colspan="3" width="100%">使用ListBox控件显示数据库中的内容如下:</td>
</tr>
<tr>
<td width="50%">
<asp:DropDownList id="SourceList" runat="server" Width="100%"></asp:DropDownList></td>
<td width="50%">
<asp:Button id="AddBtn" runat="server" Width="100px" Text="添加到列表中"></asp:Button></td>
</tr>
<tr>
<td>
<asp:ListBox id="DirectList" runat="server" Width="150px" Height="200px"></asp:ListBox>
</td>
<td valign="middle">
<br>
<asp:Button ID="DeleteBtn" Width="60" Runat="server" Text="×" CommandName="addone" Font-Bold="True"
ForeColor="Red"></asp:Button>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td colspan="3" width="100%">
<asp:Label id="SucessMessage" runat="server" ForeColor="Red" Font-Bold="True" Visible="False">请选中列表控件的数据!</asp:Label></td>
</tr>
</table>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;
/// <summary>
/// Summary description for MyDropDownList.
/// </summary>
public partial class MyDropDownList : System.Web.UI.Page
{
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["ACCESSCONNECTIONSTRING"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
///绑定源列表控件的数据
GetSourceListData();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DeleteBtn.Click += new System.EventHandler(this.DeleteBtn_Click);
this.AddBtn.Click += new System.EventHandler(this.AddBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void GetSourceListData()
{
///清空ListBox控件的所有数据项
SourceList.Items.Clear();
string AccessConnectionString = SQLCONNECTIONSTRING + Server.MapPath("csc.mdb");
///创建数据库的链接对象
String cmdText = "SELECT snid,srname from srname ORDER BY snid";
OleDbConnection myConnection = new OleDbConnection(AccessConnectionString);
OleDbCommand myCommand = new OleDbCommand(cmdText, myConnection);
///查询数据库
myConnection.Open();
OleDbDataReader reader = myCommand.ExecuteReader();
///读取SqlDataReader中的数据
while(reader.Read())
{
SourceList.Items.Add(new ListItem(reader["srname"].ToString(),reader["snid"].ToString()));
}
///关闭读取器和数据库的链接
reader.Close();
myConnection.Close();
}
private void AddBtn_Click(object sender, System.EventArgs e)
{
///添加下拉列表框中选择的数据项到列表框中
if(SourceList.SelectedIndex > -1)
{
DirectList.Items.Add(SourceList.SelectedItem.ToString());
}
///清空下拉框和列表框的选项
SourceList.SelectedIndex = -1;
DirectList.SelectedIndex = -1;
SucessMessage.Visible = false;
}
private void DeleteBtn_Click(object sender, System.EventArgs e)
{
if(DirectList.SelectedIndex > -1)
{
///删除列表框中选择的数据项
DirectList.Items.Remove(DirectList.SelectedItem);
SucessMessage.Visible = false;
}
else
{
///显示提示信息
SucessMessage.Visible = true;
}
}
}
这样做 在LISTBOX里无法添加值
可在同样的下面的程序里就可以实现 请高手大哥帮忙啊
<%@ Page language="c#" Codebehind="MyDropDownList.aspx.cs" AutoEventWireup="false" Inherits="Example_6_3.MyDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>使用DropDownList控件显示数据库中的内容</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td colspan="3" width="100%">使用ListBox控件显示数据库中的内容如下:</td>
</tr>
<tr>
<td width="50%">
<asp:DropDownList id="SourceList" runat="server" Width="100%"></asp:DropDownList></td>
<td width="50%">
<asp:Button id="AddBtn" runat="server" Width="100px" Text="添加到列表中"></asp:Button></td>
</tr>
<tr>
<td>
<asp:ListBox id="DirectList" runat="server" Width="150px" Height="200px"></asp:ListBox>
</td>
<td valign="middle">
<br>
<asp:Button ID="DeleteBtn" Width="60" Runat="server" Text="×" CommandName="addone" Font-Bold="True"
ForeColor="Red"></asp:Button>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td colspan="3" width="100%">
<asp:Label id="SucessMessage" runat="server" ForeColor="Red" Font-Bold="True" Visible="False">请选中列表控件的数据!</asp:Label></td>
</tr>
</table>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace Example_6_3
{
/// <summary>
/// Summary description for MyDropDownList.
/// </summary>
public class MyDropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label SucessMessage;
protected System.Web.UI.WebControls.ListBox DirectList;
protected System.Web.UI.WebControls.DropDownList SourceList;
protected System.Web.UI.WebControls.Button AddBtn;
protected System.Web.UI.WebControls.Button DeleteBtn;
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///绑定源列表控件的数据
GetSourceListData();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DeleteBtn.Click += new System.EventHandler(this.DeleteBtn_Click);
this.AddBtn.Click += new System.EventHandler(this.AddBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void GetSourceListData()
{
///清空ListBox控件的所有数据项
SourceList.Items.Clear();
///创建数据库的链接对象
String cmdText = "SELECT Staff_ID,Staff_Name from Staff ORDER BY Staff_ID";
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///查询数据库
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
///读取SqlDataReader中的数据
while(reader.Read())
{
SourceList.Items.Add(new ListItem(reader["Staff_Name"].ToString(),reader["Staff_ID"].ToString()));
}
///关闭读取器和数据库的链接
reader.Close();
myConnection.Close();
}
private void AddBtn_Click(object sender, System.EventArgs e)
{
///添加下拉列表框中选择的数据项到列表框中
if(SourceList.SelectedIndex > -1)
{
DirectList.Items.Add(SourceList.SelectedItem);
Response.Write(SourceList.SelectedItem);
}
///清空下拉框和列表框的选项
SourceList.SelectedIndex = -1;
DirectList.SelectedIndex = -1;
SucessMessage.Visible = false;
}
private void DeleteBtn_Click(object sender, System.EventArgs e)
{
if(DirectList.SelectedIndex > -1)
{
///删除列表框中选择的数据项
DirectList.Items.Remove(DirectList.SelectedItem);
SucessMessage.Visible = false;
}
else
{
///显示提示信息
SucessMessage.Visible = true;
}
}
}
}
小弟跪求了