you can like this:eg:
<asp:DropDownList id="ddlCountry" DataSource="<%# BindTheCountry() %>" DataTextField="Country" DataValueField="Country" runat="server" />public SqlDataReader BindTheCountry()
{
SqlConnection objConn=new SqlConnection("server=data;uid=run1;pwd=benben;database=Northwind");
SqlCommand cmdCustomers=new SqlCommand("SELECT Distinct Country FROM Customers",objConn);
cmdCustomers.CommandType=CommandType.Text;
objConn.Open();
return (cmdCustomers.ExecuteReader(CommandBehavior.CloseConnection));
}
<asp:DropDownList id="ddlCountry" DataSource="<%# BindTheCountry() %>" DataTextField="Country" DataValueField="Country" runat="server" />public SqlDataReader BindTheCountry()
{
SqlConnection objConn=new SqlConnection("server=data;uid=run1;pwd=benben;database=Northwind");
SqlCommand cmdCustomers=new SqlCommand("SELECT Distinct Country FROM Customers",objConn);
cmdCustomers.CommandType=CommandType.Text;
objConn.Open();
return (cmdCustomers.ExecuteReader(CommandBehavior.CloseConnection));
}
解决方案 »
- log4net记录日志时输出类名和方法名?
- 怎么在提交表单时,执行AJAX的异步处理方法
- 查询条件的困惑
- dropdownlist 绑定对象数组
- 急用: 关于webconfig的问题
- ================js里如何调用.net里的Session==========
- asp 一个简单的问题
- 能不能合并两个dataset,或者把一个dataset的某几列拿出组成新的dataset?谢谢
- 如何设置ASPnetMenu控件的Group为不显示?Item可以设置,Group设置在哪?
- iframe 和 HyperLinkColumn 问题
- 怎样在datagrid的每条记录前产生一个checkbox,如果选择了期中的几条,把它删除,怎么实现?
- 求助:关于asp.net中使用javascript的问题?在线等!
DropDownList1.DataValueField="id";
绑定数据库的一个字段.name
protected void Select_Changded(object sender,EventArgs e)
{
Textbox1.Text=DropDownList1.SelectedItem.Text;
}
SqlConnection Connection=new SqlConnection(.....);
Connection.Open();
SqlCommand myCommand=new SqlCommand("selectName",Connection);
myCommand.CommandType=CommandType.StoredProcedure;
myCommand.ExecuteNonQuery();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=myCommand;
DataSet ds=new DataSet();
sda.Fill(ds,"em");
Connection.Dispose();
DropDownList1.DataSource=ds;
DropDownList1.DataTextField="name";
DropDownList1.DataValueField="id";
DropDownList1.DataBind();
先建立一个listitem
ListItem li = new ListItem();
然后用你的值设置li的text和value属性
然后调用DropDownList.Items.Add(li);不知道理解对了没有……
DropDownList1.DataTextField="name";
DropDownList1.DataValueField="id";
DropDownList1.DataBind()ListItem li = new ListItem("Text","Value");
DropDownList.Items.Add(li);
~~~~~