在ListView中使用DropDownList绑定数据……好麻烦
时间:2011-05-11 来源:ゞ追忆o0ゞ
在Aspx页中的代码:
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="男">男</asp:ListItem>
<asp:ListItem Value="女">女</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
添加事件:ItemUpdating和ItemCreated事件
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e){
//在更新之前绑定DropDownList
DropDownList ddGender = (DropDownList)ListView1.Items[e.ItemIndex].FindControl("DropDownList1");
e.NewValues["Gender"] = ddGender.SelectedValue;
}
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DropDownList ddlGender = (DropDownList)e.Item.FindControl("DropDownList1");
if (ddlGender != null)
{
ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;
DataRowView rowView = (DataRowView)lvDataItem.DataItem;
if (rowView != null)
{
var userRow = (DataSetUsers.UsersRow)rowView.Row;
ddlGender.SelectedValue = userRow.Gender;
}
}
}
}
相关阅读 更多 +