![](/img/trans.png)
[英]C# Asp.Net changing DataSource for GridView using DropDownList
[英]How I an bind a DataSource to a DropDownList in a dynamic ListView in ASP.NET with C#?
我有一個ASP.NET應用程序,並使用ListView控件。 為了綁定此ListView,我使用DataTable對象,並且ListView具有以下結構:
<asp:listView ...>
<LayoutTemplate>
<ItemTemplate>
<AlternatingItemTemplate>
我在ItemTemplate和AlternatingItemTemplate中使用一個DropDownList。 我必須顯示位置。 為此,我使用XmlDataSource控件使用XML文件。 但是現在出現問題了,我必須在代碼中更新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>
我可以解決這個問題:/?
您在哪種情況下試圖找到控件? 您可以嘗試在ItemDataBound事件中執行location下拉部分,並使用此代碼(需要進行一些重構以防止多次獲取數據)
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中,將source下拉列表替換為一個簡單的下拉列表,並使用一個hiddenfield來存儲相關位置
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Location") %>' />
<asp:DropDownList ID="ListViewDropDownListLocation" runat="server" Width="90%" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.