怎么样实现把数据表格帮在MyDorpDownList
<body>
<form id= "form1 " runat= "server ">
<div align= "center ">
<asp:Repeater ID= "MyRepeater " runat= "server ">
<ItemTemplate>
<asp:DropDownList ID= "MyDropDownList " runat= "server "> </asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindDropDownList();
}
public SqlConnection CreateConn()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings[ "SqlConnString "];
con.Open();
return con;
}
public void BindDropDownList()
{
SqlConnection conn = CreateConn();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand( "select * from a_cat where sid <> 0 ",conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "a_cat ");
MyRepeater.DataSource = ds.Tables[ "a_cat "].DefaultView;
MyRepeater.DataBind();
}
}
问问,怎么样实现把表格a_cat 绑定在MyDorpDownList 上,当然Repeater上也要显示其他表的内容。
------解决方案--------------------string sql = " ";
DataSet ds = DbHelperSQL.Query(sql);
this.MyDropDownList.DataTextField = "DirNameChs ";
this.MyDropDownList.DataValueField = "DirectoryID ";
this.MyDropDownList.DataSource = ds;
this.MyDropDownList.DataBind();
只能绑定两个字段阿,一个作为Text,一个作为Value;
不能把其他更多的字段绑定上ddl了
------解决方案--------------------在Repeater的ItemDataBound事件中写
DropDownList dlist=e.Items.FindControls( "MyDropDownList ") as DropDownList
if(dlist!=null)
{
string sql = " ";
DataSet ds = DbHelperSQL.Query(sql);
this.MyDropDownList.DataTextField = "DirNameChs ";
this.MyDropDownList.DataValueField = "DirectoryID ";