Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 下拉列表SelectedIndexChanged事件处理程序在ListView控件内多次激发_Asp.net_Listview_Drop Down Menu_Selectedindexchanged - Fatal编程技术网

Asp.net 下拉列表SelectedIndexChanged事件处理程序在ListView控件内多次激发

Asp.net 下拉列表SelectedIndexChanged事件处理程序在ListView控件内多次激发,asp.net,listview,drop-down-menu,selectedindexchanged,Asp.net,Listview,Drop Down Menu,Selectedindexchanged,我使用DataTable绑定了一个列表视图控件。在ItemDataBound中,我正在创建DropDownList并以编程方式添加项,并将SelectedIndexChanged事件绑定到该下拉列表 在我的例子中,假设在ListView中创建了三行,并且在列表视图中有三个下拉列表。当我将下拉选择更改为第一个DropDownList时,然后SelectedIndexChanged触发一次。然后我在第二个DropDownList中更改选择,然后SelectedIndexChanged触发两次,然后在

我使用
DataTable
绑定了一个列表视图控件。在
ItemDataBound
中,我正在创建
DropDownList
并以编程方式添加项,并将
SelectedIndexChanged
事件绑定到该下拉列表

在我的例子中,假设在ListView中创建了三行,并且在列表视图中有三个下拉列表。当我将下拉选择更改为第一个
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的ID,然后控件再次出现在事件中,我得到了发送方对象DropDown2


我应该怎么做才能在
SelectedIndexChanged
中获得精确控件的选定项?

您需要这样做

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>

电子邮件地址
复习
发送回级别1

我已经修改了我的代码…并且它在我的本地工作…

感谢您的回答,我尝试了您建议的方法,但得到了相同的结果。i、 e.当我更改第二个下拉列表中的部分时,事件将触发两次:-(您好,先生,如果我放置
Page.IsPostBack
条件,则控件的呈现在第一次加载页面时消失。删除Page.IsPostBack是的,先生,删除条件后,输出相同,
SelectedIndexChanged
多次触发。是否可以防止多次触发处理程序?