<mySearch ⁄>
<mySnippets order="rand" ⁄>
<myContacts ⁄><email ⁄>
<windows live messenger ⁄>
<myCurriculum type="pdf" ⁄>
<myBlog show="last" ⁄>
<myNews show="rand" ⁄>
<myNews type="cat" ⁄>
<myQuote order="random" ⁄>Quem te lisonjeia é teu inimigo, e quem te critica é o teu mestre.
<myPhoto order="random" ⁄>
<myAdSense ⁄>
<myVisitorsMap ⁄>
<xml> <pedrocorreia.net> <artes_marciais> <arte_marcial> <name>taichichuan</name> <description>Tai Chi Chuan</description> </arte_marcial> <arte_marcial> <name>aikido</name> <description>Aikido</description> </arte_marcial> <arte_marcial> <name>kendo</name> <description>Kendo</description> </arte_marcial> <arte_marcial> <name>taekwondo</name> <description>Taekwon-Do</description> </arte_marcial> </artes_marciais> </pedrocorreia.net> </xml>
<!-- Application Custom Configs --> <appSettings> <add key="UrlXML" value="http://www.pedrocorreia.net/_snippets_/aspnet/dropdown_xml/xml.xml"/> </appSettings>
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// Class responsável pela preenchimento da dropdown /// </summary> public class FillDropdownList{ private DropDownList _ddl; private string _ddlText; private string _ddlValue; private string _nodeElement; private string _urlXml; /// <summary> /// Obter ou alterar o nome do campo que irá preencher o texto dos itens /// </summary> public string DDLText { get { return _ddlText; } set { _ddlText = value; } } /// <summary> /// Obter ou alterar o nome do campo que irá preencher o valor dos itens /// </summary> public string DDLValue { get { return _ddlValue; } set { _ddlValue = value; } } /// <summary> /// Obter ou alterar o endereço do ficheiro xml a ser lido /// </summary> public string UrlXml { get { return _urlXml; } set { _urlXml = value; } } /// <summary> /// Obter ou alterar o nome dos elementos que vão ser lidos /// </summary> public string NodeElement { get { return _nodeElement; } set { _nodeElement = value; } } /// <summary> /// Método construtor /// </summary> /// <param name="dropdown">Dropdown a ser preenchida</param> /// <param name="urlXml">Endereço do xml a ser lido</param> /// <param name="fieldText">Campo que possui o valor dos itens</param> /// <param name="fieldValue">Campo que possui o texto dos itens</param> /// <param name="nodeElement">Identificação do elemento onde estão guardados os itens a processar</param> public FillDropdownList(DropDownList dropdown, string urlXml, string fieldText, string fieldValue, string nodeElement) { this._ddl= dropdown; this._ddlText = fieldText; this._ddlValue = fieldValue; this._urlXml = urlXml; this._nodeElement = nodeElement; } /// <summary> /// Preencher DropdownList /// /// nota: o tratamento de erros deverá ser mais elaborado ... /// </summary> public void FillDropdown() { DataSet dataSet = new DataSet(); dataSet = new DataSet(this._nodeElement); dataSet.ReadXml(this._urlXml); this._ddl.DataSource = dataSet.Tables[this._nodeElement]; this._ddl.DataTextField = this._ddlText; this._ddl.DataValueField = this._ddlValue; this._ddl.DataBind(); this._ddl.Items.Insert(0, new ListItem("(escolha um item)", "")); dataSet.Dispose(); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Preencher DropdownList usando conteúdos fornecidos por xml</title> </head> <body> <form id="form1" runat="server"> <div> <label for="DDList" id="lblDDList" title="Escolha uma Arte Marcial">Arte Marcial</label> <asp:DropDownList ID="DDList" runat="server"></asp:DropDownList> </div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.Configuration; public partial class Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { string urlXmlService = WebConfigurationManager.AppSettings["UrlXML"].ToString(); FillDropdownList ddl = new FillDropdownList(this.DDList, urlXmlService, "description", "name", "arte_marcial"); ddl.FillDropdown(); } } }