很简单的例子,代码如下:
<%@Page Language="C#"%>
<script runat="server">
public class DropDownList: System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList dropdownlist1;
protected System.Web.UI.WebControls.Button button1;
protected System.Web.UI.WebControls.Label Label1;
void Page_load(Object Sender,EventArgs E)
{
if(!Page.IsPostBack)
{
ArrayList myArray = new ArrayList();
myArray.Add(new ListItem("a","a"));
myArray.Add(new ListItem("b","b"));
dropdownlist1.DataSource = myArray;
dropdownlist1.DataBind();
}
}
}
public void Button_Click(object sender,EventArgs e)
{
label1.Text="你选择了"+ dropdownlist1.SelectedItem.Text +".";
}
</script>
<form id="form1" runat="server">
<asp:DropDownList id="dropdownlist1" runat="server"></asp:DropDownList>
<br/><br/>
<asp:Button id="button1" Text="Submit" OnClick="Button_Click" runat="server"/>
<br/><br/>
<asp:Label id="label1" Text="" runat="server"/>
</form>
<%@Page Language="C#"%>
<script runat="server">
public class DropDownList: System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList dropdownlist1;
protected System.Web.UI.WebControls.Button button1;
protected System.Web.UI.WebControls.Label Label1;
void Page_load(Object Sender,EventArgs E)
{
if(!Page.IsPostBack)
{
ArrayList myArray = new ArrayList();
myArray.Add(new ListItem("a","a"));
myArray.Add(new ListItem("b","b"));
dropdownlist1.DataSource = myArray;
dropdownlist1.DataBind();
}
}
}
public void Button_Click(object sender,EventArgs e)
{
label1.Text="你选择了"+ dropdownlist1.SelectedItem.Text +".";
}
</script>
<form id="form1" runat="server">
<asp:DropDownList id="dropdownlist1" runat="server"></asp:DropDownList>
<br/><br/>
<asp:Button id="button1" Text="Submit" OnClick="Button_Click" runat="server"/>
<br/><br/>
<asp:Label id="label1" Text="" runat="server"/>
</form>
解决方案 »
- 如何动态设置一个文本控件的焦点?
- 关于MVC权限验证!一个控制器里边的所有都要验证?
- 如何将界面上textBox中内容写回空间中?
- You are not authorized to view this page
- MVC 确认对话框问题
- 一个repeater控件显示数据问题!
- 按特定文件名下载存在问题?在线等== = = = = = = = == = == == == == = == == ===
- “DataRowView”是一个类型,不能用作表达式。是什么意思?
- 跟踪时,难道都得在页面上写上:trace.write 什么什么变量,才能看到变量的值吗?
- DataGrid更新时出错为什么?
- vs.net的form中能否查入图片?
- 打开新页面时,为何总会在旧窗口中打开?
public void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
string MyConnString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+ConfigurationSettings.AppSettings["DataBase"];
OleDbConnection MyConn=new OleDbConnection(MyConnString);
MyConn.Open(); string sqlstr="select * from PrintType";
OleDbCommand MyComm=new OleDbCommand(sqlstr,MyConn);
//DateSet ds=new DataSet();
DataSet ds=new DataSet();
OleDbDataAdapter MyAdapter=new OleDbDataAdapter(sqlstr,MyConn);
MyAdapter.Fill(ds,"Score");
this.Brand.DataSource=ds.Tables["Score"];
this.Brand.DataTextField="small_class_data";
this.Brand.DataValueField="small_class_data";
this.Brand.DataBind();
this.Brand.Items.Insert(0,new ListItem("请选择","0"));
MyConn.Close();
}
}
dv赋值后
.......
if(dv.count>0)
{
for(int i=0;i<dv.Count;i++)
{
dropdownlist1.Items .Add(new ListItem(dv[i]["字段"].tostring()));
}
}
this.DropDownList1.DataTextField = dataSet.Tables[0].Columns[1].ColumnName;
this.DropDownList1.DataValueField = dataSet.Tables[0].Columns[0].ColumnName;
this.DropDownList1.DataBind();
dropdownlist1.DataSource = myArray;
//指定绑定的字段
this.DropDownList1.DataTextField = "字段0";//myArray中的字段
this.DropDownList1.DataValueField = "字段1";//myArray中的字段
dropdownlist1.DataBind();