在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;
}
}
}
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DropDownList ddlLinkType = (DropDownList)e.Item.FindControl("DropDownList1");
ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;
DataRowView rowView = (DataRowView)lvDataItem.DataItem;
if (ddlLinkType != null && rowView != null)
{
var linksRow = (DAL.DataSetLinks.T_LinksRow)rowView.Row;
ddlLinkType.SelectedValue = linksRow.LinkType;
}
}
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
DropDownList ddlLinkType = (DropDownList)e.Item.FindControl("DropDownList1");
e.Values["LinkType"] = ddlLinkType.SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
DropDownList ddlLinkType = (DropDownList)ListView1.Items[e.ItemIndex].FindControl("DropDownList1");
e.NewValues["LinkType"] = ddlLinkType.SelectedValue;
}
}