2013-01-19 28 views
3

我在ListView的每一行中都有一個ListView和DropDownList。您可以在該DropDownlist中對指派的人進行cahnge(DropDownList顯示來自MySql數據庫的數據)。在ListView的Dropdownlist上的selectedIndexChanged上獲取ListView項的值

更改人員時,我需要做一些數據庫操作。爲此,我需要知道在哪個ListView Row中人員已更改,我需要知道條目的值。

這裏是我的aspx文件:

<asp:ListView ID="manageAssigneesListView" runat="server" DataKeyNames="" OnItemDataBound="OnDataBound">    
       <ItemTemplate> 
        <div class="summaryRow"> 
         <asp:Label ID="customerId" runat="server"><%# Eval("customerId") %>&nbsp;</asp:Label>  
         <div style="width:200px; margin: 0px 5px; float: left;"><%# Eval("lastName") %>, <%# Eval("firstName") %></div> 
         <div style="width:120px; margin: 0px 2px; float: left;">Nr.</div> 
         <div style="width:200px; margin-left: 50px; float: right;"> 
          <asp:DropDownList runat="server" ID="selectAssignee" Width="196" AppendDataBoundItems="true" OnSelectedIndexChanged="selectAssignee_OnSelectedIndexChanged" AutoPostBack="true"> 
          </asp:DropDownList> 
         </div> 
         <div class="clear"></div> 
        </div> 
       </ItemTemplate> 
      </asp:ListView> 

這裏是我的代碼隱藏:

protected void Page_Load(object sender, EventArgs e) 
    { 
     Helper.doAuth(Session, Response); 

     if (!IsPostBack) 
     { 
      // load all customers without assignee 
      MySqlCommand cmd = new MySqlCommand(); 
      MySqlConnection con = new MySqlConnection(); 
      con.ConnectionString = Helper.CONNECTION_STRING; 
      cmd.CommandText = "SELECT customerId, lastName, firstName, assignee FROM customer WHERE assignee IS NULL ORDER BY customerId DESC"; 
      cmd.Connection = con; 
      con.Open(); 
      MySqlDataAdapter sda = new MySqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      sda.Fill(ds); 
      manageAssigneesListView.DataSource = ds; 
      manageAssigneesListView.DataBind(); 
      con.Close(); 

     } 
    } 

    protected void OnDataBound(object sender, ListViewItemEventArgs e) 
    { 
     DropDownList selectAssignee = (e.Item.FindControl("selectAssignee") as DropDownList); 

     MySqlCommand cmd = new MySqlCommand(); 
     MySqlConnection con = new MySqlConnection(); 
     con.ConnectionString = Helper.CONNECTION_STRING; 
     cmd.CommandText = "SELECT * FROM user where role = " + Helper.ROLE_SALESPERSON; 
     cmd.Connection = con; 
     con.Open(); 
     MySqlDataAdapter sda = new MySqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     sda.Fill(ds); 
     selectAssignee.DataSource = ds; 
     selectAssignee.DataTextField = "emailAdress"; 
     selectAssignee.DataBind(); 
     con.Close();  
    } 


    protected void selectAssignee_OnSelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList dropDownList = (DropDownList)sender; 
     ListViewItem listView = (ListViewItem)dropDownList.NamingContainer; 

     var test = listView.DataItem; 
     string test2 = listView.FindControl("customerId").ToString(); 

     int rowIndex = (int)listView.DataItemIndex; 
     Label lblMessage = (Label)listView.FindControl("customerId"); 
    } 

我成功獲得該行的DataItemIndex我改變了DropDownList的價值,也DROPDOWNLIST正在觸發糾正onSelectedIndexChanged,但我需要的是標籤「customerId」的價值,我不知道如何得到這個值。

在此先感謝

回答

3

您需要使用FindControl獲得參考Label,然後用它的Text屬性:

ListViewItem item = (ListViewItem)dropDownList.NamingContainer; 
Label lbltCustomerID = (Label) item.FindControl("customerId"); 
string customerID = lblCustomerID.Text; 

順便說一句,在ListViewItem.DataItem總是null上回發。

編輯

應設置標籤的Text,所以不是

<asp:Label ID="customerId" runat="server"> 
    <%# Eval("customerId") %>&nbsp; 
</asp:Label>  

<asp:Label ID="customerId" runat="server" 
    Text='<%# Eval("customerId") %>' > 
</asp:Label>  
+0

嗨,感謝您的回答,但我確實使用了FindControl,就像您一樣。字符串customerID仍爲空。 – user1814545

+0

@ user1814545:改爲設置「Text」屬性。相應地編輯我的答案。 –

+0

對不起,那是我的錯。通過文本屬性,您的解決方案非常完美。非常感謝你。 – user1814545

1

嘗試直接從獲取標籤的價值ListView like

 DropDownList dropDownList = (DropDownList)sender; 
     ListViewItem listView = (ListViewItem)dropDownList.NamingContainer; 

     var test = listView.DataItem; 
     string test2 = listView.FindControl("customerId").ToString(); 

     int rowIndex = (int)listView.DataItemIndex; 
     Label label = (Label)manageAssigneesListView.Items[rowIndex ].Controls[0]; 

Label label = (Label)manageAssigneesListView.Items[rowIndex].FindControl("customerId"); 

,並確保ListView控件不回發綁定,每次

一樣,如果裝上pageLoad的ListView中不要忘了將它添加在:

if (!IsPostBack) 
    { 
     //Load ListView 
    } 
1

即使回答問題,我的解決方案對我來說效果很好:

protected void myDdl_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList dropDownList = (DropDownList)sender; 
     ListViewDataItem listView = (ListViewDataItem)dropDownList.NamingContainer; 
     int rowIndex = listView.DataItemIndex; 
     //Some logic 
    } 

不是我使用ListViewDataItem Peistemaker的解決方案的舊版本