string CatalogGuidSql = "select CatalogName,CatalogGuid from ProductCatalogs where [Language]='" + dr["Code"].ToString() + "'";
DataTable dt2 = Mydata.GetTable(CatalogGuidSql);
foreach (DataRow dr2 in dt2.Rows)
{
CatalogGuids.Append("<option value=" + dr2["CatalogGuid"] + ">" + dr2["CatalogName"] + "</option>");
}
TableCell cell = new TableCell();
DropDownList CatalogGuid = new DropDownList();
CatalogGuid.ID = "CatalogGuid";
CatalogGuid.CssClass = "input";
CatalogGuid.DataSource = CatalogGuids;
//这样绑定是错的,提示:数据源的类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。
CatalogGuid.DataBind();
cell.Controls.Add(CatalogGuid);请问应该如何绑定它的数据源啊?
DataTable dt2 = Mydata.GetTable(CatalogGuidSql);
foreach (DataRow dr2 in dt2.Rows)
{
CatalogGuids.Append("<option value=" + dr2["CatalogGuid"] + ">" + dr2["CatalogName"] + "</option>");
}
TableCell cell = new TableCell();
DropDownList CatalogGuid = new DropDownList();
CatalogGuid.ID = "CatalogGuid";
CatalogGuid.CssClass = "input";
CatalogGuid.DataSource = CatalogGuids;
//这样绑定是错的,提示:数据源的类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。
CatalogGuid.DataBind();
cell.Controls.Add(CatalogGuid);请问应该如何绑定它的数据源啊?
<asp:TemplateColumn HeaderText="...">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSource='<%#Users/GetUsers()/GetUsers(Eval("ClassID").ToString()) %>'
DataTextField="UserName" DataValueField="UserID"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
谢谢,但是dt2里面字段太多了啊,DropDownList只用到两个字段嘛
string CatalogGuidSql = "select CatalogName,CatalogGuid from ProductCatalogs where [Language]='" + dr["Code"].ToString() + "'";
DataTable dt2 = Mydata.GetTable(CatalogGuidSql);
DropDownList CatalogGuid = new DropDownList();
TableCell cell = new TableCell();
foreach (DataRow dr2 in dt2.Rows)
{
CatalogGuid.Items.Add(new ListItem(dr2["CatalogName"].ToString(),dr2["CatalogGuid"].ToString()))
}
CatalogGuid.CssClass = "input";
cell.Controls.Add(CatalogGuid);
方法二
string CatalogGuidSql = "select CatalogName,CatalogGuid from ProductCatalogs where [Language]='" + dr["Code"].ToString() + "'"; DataTable dt2 = Mydata.GetTable(CatalogGuidSql);
DropDownList CatalogGuid = new DropDownList();
TableCell cell = new TableCell(); CatalogGuid.DataTextFiled = "CatalogName";
CatalogGuid.DataValueFiled = "Catalogid"
CatalogGuid.CssClass = "input";
CatalogGuid.DataSource = CatalogGuids;
cell.Controls.Add(CatalogGuid);