Generieren DropDownList aus dem code hinter

Hallo, ich bin die Generierung einer DropDownList in meinem code-behind-Datei

    protected DropDownList CountryList() 
    {
        DropDownList ddl = new DropDownList();

        XDocument xmlDoc = XDocument.Load(Server.MapPath("Countries.xml"));
        var countries = from country in xmlDoc.Descendants("Country")
                        select new
                        {
                          Name = country.Element("name").Value,                              
                        };

        foreach (var c in countries)
        {
            ddl.Items.Add(c.Name);                
        }
        return ddl;
    }

Hatte ich Träume, dann mit <%= CountryList() %> auf meiner aspx-Seite. Aber wenn ich das mache druckt es die Zeichenfolge "System.Web.UI.WebControls.DropDownList".

Kann ich machen das Weg, es zu tun die Arbeit, oder muss ich das setup ein ContentPlaceHolder und fügen Sie dann die DropDownList den Inhalt?

Cheers

  • Jubel für die Kommentare, Danke
InformationsquelleAutor Marklar | 2009-06-15
Schreibe einen Kommentar