0

我有一个 ASP.NET 应用程序并使用 ListView 控件。为了绑定这个 ListView,我使用了一个 DataTable 对象,我的 ListView 具有以下结构:

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

我在 ItemTemplate 和 AlternatingItemTemplate 中使用了一个 DropDownList。这必须我显示位置。为此,我使用 XML 文件和 XmlDataSource 控件。但现在的问题是我必须在代码中更新我的 XML 文件并删除我只有活动用户的特殊元素的元素。这意味着我的 ListView 中的 DropDoanList 控件不会显示 XML 文件中的所有元素。

所以我想我可以先创建 DataTable 对象。然后我将它与 ListView 绑定,然后在 ListView 中找到控件。但我每次都得到“null”:(。我找不到这个对象。

这是我的代码:

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

1 回答 1

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 替换为 source 到一个简单的下拉列表和一个隐藏字段来存储相关位置

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Location") %>' />
<asp:DropDownList ID="ListViewDropDownListLocation" runat="server" Width="90%" />
于 2013-10-14T08:35:31.097 回答