我有一个ASP.NET应用程序并使用ListView控件。为了绑定这个ListView我使用DataTable对象,我的ListView有以下结构:
<asp:listView ...>
<LayoutTemplate>
<ItemTemplate>
<AlternatingItemTemplate>
我在ItemTemplate和AlternatingItemTemplate中使用DropDownList。这必须我显示位置。我使用XML文件与XmlDataSource控件。但现在是问题我必须在代码中更新我的XML文件并删除我只有Active用户的特殊元素的元素。这意味着我的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>
何我可以解决这个问题:/?
答案 0 :(得分: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中将locationdowndown与source替换为简单的下拉列表和hiddenfield来存储相关位置
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Location") %>' />
<asp:DropDownList ID="ListViewDropDownListLocation" runat="server" Width="90%" />