2014-06-20 66 views
1

我有一個列表視圖控件,我使用DataTable進行綁定。在ItemDataBound中,我創建了DropDownList,並以編程方式添加項並將SelectedIndexChanged事件綁定到該下拉列表。下拉列表SelectedIndexChanged事件處理程序在ListView控件內部多次觸發

在我的情況下,假設在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的選定值該事件觸發兩次,並在第一次我得到發件人對象ID的DrowDown1,然後再次控制進來的事件,我得到發件人對象DropDown2。

我該怎麼做才能獲得SelectedIndexChanged中的精確控制選定項目?

回答

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> 

我已經修改我的代碼......它的工作在我的地方......

+0

感謝您的回答,我嘗試了您的建議方式,但獲得了相同的結果。即當我將第二個下拉菜單中的部分更改下來時,事件觸發兩次。 :-( –

+0

您可以發佈您的修改後的代碼.. –

+0

您好先生,如果我放置了'Page.IsPostBack'條件,那麼當第一次頁面加載時控件的呈現消失 –

相關問題