2013-10-14 78 views
0

我有一個ASP.NET應用程序並使用ListView控件。爲了結合這個ListView中我使用了一個DataTable對象和我的ListView有個後續結構:如何使用C#將ASP.NET數據源綁定到ASP.NET動態ListView中的DropDownList?

<asp:listView ...> 
<LayoutTemplate> 
<ItemTemplate> 
<AlternatingItemTemplate> 

我在ItemTemplate和AlternatingItemTemplate一個DropDownList使用。這必須顯示位置。我用XmlDataSource控件爲此使用了一個XML文件。但現在是問題,我必須在代碼中更新我的XML文件,並刪除我只有Active用戶的特殊元素的元素。這意味着我的ListView中的DropDoanList控件不顯示XML文件中的所有元素。

所以我想我可以先創建DataTable對象。然後我將它與ListView綁定,然後在ListView中查找控件。但我每次得到「空」 :(我不覺得這個對象

這裏是我的代碼:

ListView.DataSource = tb; 
       ListView.DataBind(); 

       XDocument x = XDocument.Load(Server.MapPath(@"~\App_Data\location.xml")); 

       string ActiveUser = GetUsername(); 

       ArrayList ListOfNPSGroups = GetGroupsInOUByValue(); 

       ArrayList ActiveUserList = GetGroupmemberList(ListOfNPSGroups); 

       x.Root.Descendants() 
            .Where(d => !ActiveUserList.Contains((string)d.Attribute("group"))) 
            .ToList() 
            .ForEach(s => s.Remove()); 

       var data = (from item in x.Elements("plants").Elements("plant") 
          select new { display = item.Attribute("display").Value, id = item.Attribute("id").Value }).ToList(); 

       DropDownList ListViewDropDownListLocation = (DropDownList)ListView.FindControl("ListViewDropDownListLocation"); // here I get NULL 

       ListViewDropDownListLocation.DataSource = data; 
       ListViewDropDownListLocation.DataTextField = "display"; 
       ListViewDropDownListLocation.DataValueField = "id"; 
       ListViewDropDownListLocation.DataBind(); 

在這裏,我表明我的ASPX:

<ItemTemplate> 
        <tr id="Tr1" class="TableClassO" runat="server" onmouseover="this.style.backgroundColor='#87CEFA'" 
        onmouseout="this.style.backgroundColor='#ffffff'" titel="Auswahl"> 

         <td> 
          <asp:DropDownList ID="drpDeviceClass" runat="server" SelectedValue='<%# Eval("DeviceClass") %>' DataTextField="display" DataValueField="id" DataSourceID="xmlDeviceClass" Width="90%" > 
          </asp:DropDownList> 
          <asp:XmlDataSource ID="xmlDeviceClass" runat="server" DataFile="~/App_Data/devices.xml" ></asp:XmlDataSource> 
         </td> 

         <td > 
          <asp:TextBox ID="txtMacAdress" runat="server" Text='<%# Eval("MAC") %>' Width="90%"></asp:TextBox> 
         </td> 

         <td > 
          <asp:DropDownList ID="ListViewDropDownListLocation" SelectedValue='<%# Eval("Location") %>' runat="server" Width="90%" 
          DataTextField="display" DataValueField="id" DataSourceID="ListViewXMLRessourceLocation"></asp:DropDownList> 
          <asp:XmlDataSource ID="ListViewXMLRessourceLocation" runat="server" DataFile="~/App_Data/location.xml" ></asp:XmlDataSource> 
         </td> 

         <td > 
          <asp:TextBox ID="txtFirstname" runat="server" Text='<%# Eval("Vorname") %>' Width="90%"></asp:TextBox> 
         </td> 

         <td > 
          <asp:TextBox ID="txtLastname" runat="server" Text='<%# Eval("Nachname") %>' Width="90%"></asp:TextBox> 
         </td> 

         <td > 
          <asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Beschreibung") %>' Width="90%"></asp:TextBox> 
         </td> 
         <td> 
          <asp:ImageButton ID="imgSaveOnly" ImageAlign="Middle" runat="server" CommandName="Save" CommandArgument='<%# Container.DataItemIndex %>' Width="15" Height="15" ImageUrl="~/App_Themes/Images/Save-icon.png" ToolTip="Eintrag ins Active Directory übernehmen" /> 
         </td> 
         <td> 
          <asp:ImageButton ID="imgPowerShell" ImageAlign="Middle" runat="server" CommandName="Powershell" CommandArgument='<%# Container.DataItemIndex %>' Width="15" Height="15" ImageUrl="~/App_Themes/Images/ps.png" ToolTip="PowerShell Befehl Anzeigen" /> 
         </td> 

         <td> 
          <asp:ImageButton ID="imgDelete" runat="server" ImageAlign="Middle" CommandName="Delete" CommandArgument='<%# Container.DataItemIndex %>' Width="15" Height="15" ImageUrl="~/App_Themes/Images/delete.png" ToolTip="Eintrag Löschen" /> 
         </td> 

        </tr> 
       </ItemTemplate> 

浩,我可以解決這個問題問題:/?

回答

4

在哪個事件中,您是否在嘗試查找控件?您可以嘗試在ItemDataBound事件中執行位置下拉部分並使用此代碼(需要進行一些重構以不能多次獲取數據巧妙地)

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    XDocument x = XDocument.Load(Server.MapPath(@"~\App_Data\location.xml")); 
    string ActiveUser = GetUsername(); 
    ArrayList ListOfNPSGroups = GetGroupsInOUByValue(); 
    ArrayList ActiveUserList = GetGroupmemberList(ListOfNPSGroups); 
    x.Root.Descendants().Where(d => !ActiveUserList.Contains((string)d.Attribute("group"))) 
         .ToList() 
         .ForEach(s => s.Remove()); 

    var data = (from item in x.Elements("plants").Elements("plant") 
       select new { display = item.Attribute("display").Value, id = item.Attribute("id").Value }).ToList(); 
    HiddenField hidden = e.Item.FindControl("HiddenField1") as HiddenField; 
    if (hidden != null && !string.IsNullOrEmpty(hidden.Value)) 
    { 
    DropDownList listViewDropDownListLocation = e.Item.FindControl("ListViewDropDownListLocation") as DropDownList; 

    listViewDropDownListLocation.DataSource = data; 
    listViewDropDownListLocation.DataTextField = "display"; 
    listViewDropDownListLocation.DataValueField = "id"; 
    listViewDropDownListLocation.DataBind(); 
    listViewDropDownListLocation.SelectedValue = hidden.Value; 
    } 
} 

而且在.aspx替換源的locationdropdown到一個簡單的下拉和hiddenfield來存儲相關位置

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Location") %>' /> 
<asp:DropDownList ID="ListViewDropDownListLocation" runat="server" Width="90%" /> 
+0

存在的問題是,我有一個DataTable用於在ListView結合。首先我創建這個DataTable,然後將它綁定到我的ListView。但後來我得到一個錯誤,我不能使用DropDownList中的SelectedValue,因爲它不存在一個列表。 :/ – Tarasov

+0

我改變了答案,這不是很好,也許它會工作 –

+0

好的,謝謝我試試這.... – Tarasov

相關問題