![](/img/trans.png)
[英]With a custom control that's inside of an ItemTemplate (Repeater), how can I access values from the repeater item?
[英]How can I edit the DropDown item in the Repeater control
在此我在Repeater 控件中添加了一个DropDownList,为此将一个DataTable 分配为DataSource。
但我想根据 DataSource 数据编辑 DropDownList.Items。
表示如果 DataSource 将提供 3 个数据,则 DropDownLidt 具有来自 1、2、3 的列表项,如果是 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());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.