我已经设置了我认为正确的标记和代码,以便在转发器的标题模板中放置一个下拉列表,但由于某种原因,它无法正常工作。最初我看到下拉列表,但是当我在列表中进行选择时,OnSelectedIndexChanged 事件似乎没有触发使用添加的方法来处理它,我无法弄清楚我错过了什么,因为我认为这个这是一件简单的事情。
以下是标记的要点:
<asp:Repeater ID="repNewsItems" runat="server">
<HeaderTemplate>
<asp:DropDownList ID="ddDateSortCategories" runat="server" Width="200" AutoPostBack="true" OnSelectedIndexChanged="ddDateSortCategories_SelectedIndexChanged1" CssClass="mergersDropDown">
<asp:ListItem Selected="True" Text="Most Recent" Value="newest"></asp:ListItem>
<asp:ListItem Text="Oldest" Value="oldest"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
// other stuff
以下是背后代码的核心内容:
[MacroParameter(MacroParameterType.Number)]
public int PageId { get; set; }
public string _category = "";
protected void Page_Load(object sender, EventArgs e)
{
if (repNewsItems == null) return;
_category = Request.QueryString["category"];
repNewsItems.ItemDataBound += new RepeaterItemEventHandler(repNewsItems_ItemDataBound);
repNewsItems.DataSource = (string.IsNullOrEmpty(NewsNodeIds))
? GetNewsItemsForYear(NewsYearToDisplay, _category)
: DaNews;
repNewsItems.DataBind();
}
protected void ddDateSortCategories_SelectedIndexChanged1(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)(sender);
Response.Redirect(umbraco.library.NiceUrl(PageId) + "?category=" + Server.UrlEncode(ddl.SelectedValue.ToString()));
}
void repNewsItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddDateSortCategories");
if (!IsPostBack)
{
if (ddl != null)
{
ddl.DataSource = GetNewsItemsForYear(DocumentTypes.NewsItem.GetMostRecentYear(NewsCategoryToDisplay).ToString(), _category);
ddl.DataBind();
}
}
if (!String.IsNullOrEmpty((Request.QueryString["category"]))) ddl.Text = Request.QueryString["category"];
}
// other stuff
}