我写了下面的代码
受保护的无效ListView_Articles_ItemDataBound(对象发件人,ListViewItemEventArgs e){
if (e.Item.ItemType == ListViewItemType.DataItem)
{
if (cmd == "edit")
{
// Display the e-mail address in italics.
int month, year;
month = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem,"Created")).Month;
year = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem, "Created")).Year;
ListViewDataItem item = (ListViewDataItem)e.Item;
DropDownList ddlmonth = (DropDownList)e.Item.FindControl("ddlmonth");
DropDownList ddlyear = (DropDownList)e.Item.FindControl("ddlyear");
ListItem lstitem = ddlyear.Items.FindByValue(year.ToString());
// 我发现 ddlyear 是 null 所以它无法绑定数据。
if (ddlmonth != null)
{
foreach (ListItem monthitem in ddlmonth.Items)
{
if (int.Parse(monthitem.Value) == month)
{
ddlmonth.ClearSelection();
monthitem.Selected = true;
return;
}
}
}
if (ddlyear != null)
{
foreach (ListItem yearitem in ddlyear.Items)
{
if (int.Parse(yearitem.Value) == year)
{
ddlyear.ClearSelection();
yearitem.Selected = true;
return;
}
}
}
}
}
}