我有一个列表视图控件,我使用DataTable
进行绑定。在ItemDataBound
我正在创建DropDownList
并以编程方式添加项目并将SelectedIndexChanged
事件绑定到该下拉列表。
在我的例子中,假设在ListView中创建了三行,并且列表视图中有三个Drop Downs。当我将下拉选项更改为第一个DropDownList
时,SelectedIndexChanged
会触发一次。然后我在第二个DropDownList
中更改选择,然后SelectedIndexChanged
两次触发,然后我在第二个DropDownList
中更改选择,然后SelectedIndexChanged
三次触发。
protected void dlMain_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView currentRow = (DataRowView)e.Item.DataItem;
Panel pnl = (Panel)e.Item.FindControl("pnlUser");
if (pnl != null)
{
pnl.Controls.Add(new LiteralControl(currentRow.Row.ItemArray[1].ToString()));
}
Panel pnlDropDown = (Panel)e.Item.FindControl("pnlDropDown");
if (pnlDropDown != null)
{
if (dtCustomers.Rows.Count > 0)
{
DataRow[] customers = dtCustomers.Select("KItemId = " + Convert.ToInt32(currentRow.Row.ItemArray[0]) + "");
if (customers.Length > 0)
{
DropDownList customerDDL = new DropDownList();
ListItem baseItem = new ListItem("-- Select --", "0");
customerDDL.Items.Add(baseItem);
foreach (DataRow customerRow in customers)
{
ListItem newItem = new ListItem(customerRow["CustName"].ToString(), customerRow["CustId"].ToString());
customerDDL.Items.Add(newItem);
}
customerDDL.ID = "Customers" + Convert.ToString(currentRow.Row.ItemArray[0]);
customerDDL.AutoPostBack = true;
customerDDL.SelectedIndexChanged += customerDDL_SelectedIndexChanged;
pnlDropDown.Controls.Add(customerDDL);
}
}
}
}
}
要检查事件的行为,我在SelectedIndexChanged
事件
void customerDDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList changedList = (DropDownList)sender;
}
这里当我第一次更改DropDown1的选定值时,我得到ID
DropDown1然后我更改DropDown2的选定值,事件触发两次,并且在第一次我得到发送者对象DrowDown1和然后再次控制来自事件,我得到发件人对象DropDown2。
如何在SelectedIndexChanged
中获取确切控件的所选项?
答案 0 :(得分:1)
你需要像这样做......
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int[] arr = { 1, 3, 5 };
ListView1.DataSource = arr;
ListView1.DataBind();
}
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
ddlval.Text = ((DropDownList)sender).SelectedValue;
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem )
{
DropDownList ddl = e.Item.FindControl("ddl") as DropDownList;
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
}
}
ASPX页面
<asp:listview id="ListView1" runat="server" onitemdatabound="ListView1_ItemDataBound" xmlns:asp="#unknown">
<LayoutTemplate>
<table cellpadding="2" width="680px" border="0">
<tr id="Tr1" style="background-color: #ADD8E6" runat="server">
<th id="Th3" runat="server">
E-mail Address
</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<itemtemplate>
<tr id="Tr2" style="background-color: #CAEEFF" runat="server">
<td>
<asp:dropdownlist id="ddl" runat="server" autopostback="true" onselectedindexchanged="ddl_SelectedIndexChanged">
<asp:listitem text="Select 1" value="One" selected="True">Review</asp:listitem>
<asp:listitem text="Select 2" value="Two">Send Back to Level1</asp:listitem>
</asp:dropdownlist>
</td>
</tr>
</itemtemplate>
</asp:listview>
<asp:label id="ddlval" text="test" runat="server"></asp:label>
我修改了我的代码......它正在我的本地工作......