简体   繁体   中英

How I an bind a DataSource to a DropDownList in a dynamic ListView in ASP.NET with C#?

I have a ASP.NET Application and use the ListView Control. For binding this ListView I use a DataTable object and my ListView have th follow structure:

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

I use in the ItemTemplate and AlternatingItemTemplate a DropDownList. This must me show the Location. I use a XML File for this with XmlDataSource Control. But now is the Problem I must update my XML File in the code and delete Elements that I have only Special Elements for the Active user. That means the DropDoanList Control in my ListView don't show all Elements in the XML File.

So I think I can create first the DataTable Object. Then I bind this with the ListView and after this I Find The Control in the ListView. But I get everytime "null" :( . I dont find this object.

here is my code:

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();

Here I show my 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>

Ho I can solve this Problem :/ ?

In which event are you trying to find the control? You could try to do the location dropdown part in the ItemDataBound event and use this code (needs some refactoring to not get data multiple times probably)

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;
    }
}

And in the .aspx replace the locationdropdown with source to a simple dropdown and a hiddenfield to store the relevant location

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM