Repeater中绑定DropDownList
效果如图:
后台代码如下 :
/// <summary>
/// 动态绑定Repeater中的指派下拉框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList dllOperator = (DropDownList)e.Item.FindControl("dllOperator");
dllOperator.DataSource = AdminUserService.GetList("");
dllOperator.DataTextField = "realName";
dllOperator.DataValueField = "id";
dllOperator.DataBind();
dllOperator.Items.Insert(0,new ListItem("请选择", "-99"));
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;//获取当前项行数据再去访问那一列.注意坐标索引是从0开始
dllOperator.SelectedValue = rowv[4].ToString();
}
}
前台代码:
<asp:Repeater ID="repInfo" runat="server" OnItemDataBound="rptContent_ItemDataBound">
<asp:DropDownList ID="dllOperator" runat="server"></asp:DropDownList>