给出一个asp转发器和它内部的下拉列表。 我正在转发器上做一个数据绑定并有一个ItemDataBound事件。 事件被触发我将下拉列表绑定到某些数据源并使用autopostback true分配SelectedIndexChanged处理程序。
为每个下拉列表调用处理程序(在我的情况下我有5个)但是对于每个下拉列表,“sender”始终是第一个。
观点:
<asp:Repeater ID="OptionsRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions" runat="server">
</ItemTemplate>
</asp:Repeater>
转发器数据源代码正常:
OptionsRepeater.DataSource = someDataSource;
OptionsRepeater.ItemDataBound += new RepeaterItemEventHandler(this.OptionsRepeaterItemDataBound);
OptionsRepeater.DataBind();
事件处理程序:
protected virtual void OptionsRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Get the view class for the current repeater record
var dropDownData = e.Item.DataItem;
DropDownList dropDownList = (DropDownList)e.Item.FindControl("ddlOptions");
if (dropDownList != null)
{
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = "Title";
dropDownList.DataValueField = "Id";
dropDownList.AutoPostBack = true;
dropDownList.DataBind();
dropDownList.SelectedIndexChanged += DropDownListSelectedIndexChanged;
}
}
}
protected void DropDownListSelectedIndexChanged(object sender, EventArgs e)
{
//((DropDownList)sender).UniqueID is always the id of the first combo changed and never on the real one.
}
答案 0 :(得分:2)
好吧无论如何它正在运作。 它之所以不起作用的原因是在数据绑定之后和选择之后更改了事件处理程序它被赋值我接触了数据,使得.selected = true对其中一些:)
所以我的错误归功于提示。
如果有人遇到同样的问题,仍然是一个完全有效的解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DoDataBind();
}
private void DoDataBind()
{
List <List <Tuple<string, string>>> someDataSource = new List<List<Tuple<string, string>>>();
someDataSource.Add(new List<Tuple<string, string>>
{
new Tuple<string, string>("item1", "item1"),
new Tuple<string, string>("item2", "item2"),
new Tuple<string, string>("item3", "item3")
});
someDataSource.Add(new List<Tuple<string, string>>
{
new Tuple<string, string>("item4", "item4"),
new Tuple<string, string>("item5", "item5"),
new Tuple<string, string>("item6", "item6")
});
someDataSource.Add(new List<Tuple<string, string>>
{
new Tuple<string, string>("item7", "item7"),
new Tuple<string, string>("item8", "item8"),
new Tuple<string, string>("item9", "item9")
});
OptionsRepeater.DataSource = someDataSource;
OptionsRepeater.EnableViewState = false;
OptionsRepeater.ItemDataBound += this.OptionsRepeaterItemDataBound;
OptionsRepeater.DataBind();
}
/// <summary>
/// Optionses the repeater item data bound.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs" /> instance containing the event data.</param>
private void OptionsRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Get the view class for the current repeater record
var dataSource = e.Item.DataItem;
DropDownList dropDownList = (DropDownList)e.Item.FindControl("ddlOptions");
if (dropDownList != null)
{
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = "Item1";
dropDownList.DataValueField = "Item2";
dropDownList.EnableViewState = false;
dropDownList.AutoPostBack = true;
dropDownList.DataBind();
dropDownList.SelectedIndexChanged += DropDownListSelectedIndexChanged;
}
}
}
/// <summary>
/// Drops down list selected index changed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
private void DropDownListSelectedIndexChanged(object sender, EventArgs e)
{
result.Text += ((DropDownList)sender).UniqueID + "<br/>";
}
}
}
和视图:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="OptionsRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions" runat="server"> </asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
<asp:Literal ID="result" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
P.S。如果启用了视图状态,则不会正确触发事件,因此请在转发器和下拉列表中禁用状态。