在此我在Repeater控件中添加了一个DropDownList, 为此将一个DataTable分配为DataSource。如何编辑Repeater控件中的DropDown项目
但我想根据DataSource数据编辑DropDownList.Items。
意味着如果数据源将从1,2,3 给3个数据,则DropDownLidt有列表项,如果这是5则1,2,3,4,5这样
所以对于我必须使用哪个事件以及我应该写什么代码?
在此我在Repeater控件中添加了一个DropDownList, 为此将一个DataTable分配为DataSource。如何编辑Repeater控件中的DropDown项目
但我想根据DataSource数据编辑DropDownList.Items。
意味着如果数据源将从1,2,3 给3个数据,则DropDownLidt有列表项,如果这是5则1,2,3,4,5这样
所以对于我必须使用哪个事件以及我应该写什么代码?
在你的Repeater
的itemdatabound
,找到你的control
,并绑定到一个数据库,或设置值,或任何你想要的,如下图所示:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = e.Item.DataItem as DataRowView;
DropDownList dl = e.Item.FindControl("ddlCategory") as DropDownList;
dl.DataSource = CategoriesDataTable;
dl.DataTextField = "CategoryDescription";
dl.DataValueField = "CategoryPK";
dl.SelectedValue = row["CategoryFK"].ToString();
dl.DataBind();
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int count = 0;
// set count = your datatable count
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
for(int i=1;i<=count;i++)
{
ddl.Items.Add(i.ToString());
}
}
}