Pregunta

¿Cómo vincular SiteMap a un TreeView creado dinámicamente en tiempo de ejecución?

¿Fue útil?

Solución

Hay un par de formas de hacer esto.

Coloque un PlaceHolder en la página:

  <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Ahora cree un TreeView y asigne un SiteMapDataSource que ya esté en la página:

  //Code Behind
  TreeView tv1 = new TreeView();
  tv1.DataSourceID = "SiteMapDataSource1";
  PlaceHolder1.Controls.Add(tv1);

  //aspx
  <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

O puede asignar el SiteMap mediante programación:

  // Create an instance of the XmlSiteMapProvider class.
  XmlSiteMapProvider testXmlProvider = new XmlSiteMapProvider();
  NameValueCollection providerAttributes = new NameValueCollection(1);
  providerAttributes.Add("siteMapFile", "Web2.sitemap");

  // Initialize the provider with a provider name and file name.
  testXmlProvider.Initialize("testProvider", providerAttributes);

  // Call the BuildSiteMap to load the site map information into memory.
  testXmlProvider.BuildSiteMap();

  SiteMapDataSource smd = new SiteMapDataSource();
  smd.Provider = testXmlProvider;

  TreeView tv2 = new TreeView();
  tv2.DataSource = smd;
  tv2.DataBind(); //Important or all is blank
  PlaceHolder1.Controls.Add(tv2);

La configuración de SiteMap mediante programación también le permite cambiar archivos en función de las reglas comerciales.

Esto también se puede hacer a través de la Web. Configuración:

  <configuration>
  <!-- other configuration sections -->
    <system.web>
     <!-- other configuration sections -->
     <siteMap>
       <providers>
        <add name="SiteMap1" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Web.sitemap" />
        <add name="SiteMap2" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Web2.sitemap" />
       </providers>
    </siteMap>
   </system.web>
  </configuration>

y luego en su página aspx simplemente cambie de proveedor:

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" SiteMapProvider="SiteMap2"  />

Espero que esto ayude

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top