Mobile Content

Friday, April 13, 2012

Set Data Dynamically in Treeview Menu Control

I have used XML file to set data into Treeview control dynamically

Data is fetch from database category and subcategory
XMLFile.xml is a file i have taken in page and set it with XmlDataSource DataFile.

Here xml file is empty, it get fill after execution.

Green color text Fetch data from (catname or productname) from xml file.
Black color text set datamember with tag name.
 eg.
<Cagtegory catname="abc"/>
<product productname="pqr"/>

Code:

In aspx page

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="XMLFile.xml"></asp:XmlDataSource>

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1"
CssClass="TreeView1" NodeWrap="false" ShowExpandCollapse="False" NodeIndent="15" NodeStyle-ChildNodesPadding="1" NodeStyle-VerticalPadding="5">
<DataBindings>
<asp:TreeNodeBinding
DataMember="cat" Text="Select Product" PopulateOnDemand="false" />
<asp:TreeNodeBinding
DataMember="Category" TextField="Catname" PopulateOnDemand="false" />
<asp:TreeNodeBinding
DataMember="product" TextField="productname" NavigateUrlField="navi" />
</DataBindings>
<SelectedNodeStyle BackColor="Black" Font-Size="larger" Font-Names="garamond" />
<%-- <LeafNodeStyle Font-Size="large" Font-Names="garamond" /> for child--%>
<ParentNodeStyle ForeColor="DarkGray" Font-Names="garamond" Font-Size="20px"/> <%--for parent node--%>
<%--<NodeStyle BackColor="Yellow"/> for all node--%>
</asp:TreeView>

In .vb page

 Dim ds, ds1 As DataSet
    Dim sql, sql1 As New SqlConnection
    Dim cmd, cmd1 As New SqlCommand
    Dim da, d1 As New SqlDataAdapter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'select c.catname,p.productname from category c,products p where c.id=p.catid
Dim strMyXml As String = ""
Dim strMyXml2 As String = ""
Dim strMyXml1 As String = ""
 sql = New SqlConnection("
 your connection
")
cmd = New SqlCommand("
select catname,id from category
", sql)
        da = New SqlDataAdapter(cmd)
        da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
strMyXml2 = "<?xml version='1.0' encoding='utf-8' ?><cat>"
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
strMyXml = strMyXml + "<Category Catname='" + ds.Tables(0).Rows(i).ItemArray(0).ToString() + "'>" + strMyXml1 + "</Category> "
 
        cmd1 = New SqlCommand("
select productname,id from products where catid='" + ds.Tables(0).Rows(i).ItemArray(1).ToString() + "'
", sql)
        da1 = New SqlDataAdapter(cmd)
        da1.Fill(ds1)

If ds1.Tables(0).Rows.Count > 0 Then
For j As Integer = 0 To ds1.Tables(0).Rows.Count - 1
strMyXml1 = strMyXml1 + "<product productname='" + ds1.Tables(0).Rows(j).ItemArray(0).ToString() + "' navi='treeview.aspx?id=" + ds1.Tables(0).Rows(j).ItemArray(1).ToString() + "' />"
   Next
End If
Next
strMyXml2 = strMyXml2 + strMyXml + "</cat>"
Dim xDoc As New XmlDocument

xDoc.LoadXml(strMyXml2)
xDoc.Save(Server.MapPath("XMLFile.xml"))

End If
End Sub

after execution

XmlFile.xml get fill

output

<?xml version="1.0" encoding="utf-8"?>
<cat>
  <Category Catname="Indian Marble">
<product productname="Australian White" navi="treeview.aspx?id=2" />
<product productname="Beige Travertine" navi="treeview.aspx?id=3" />
<product productname="Black Markino" navi="treeview.aspx?id=4" />
<product productname="Black Pearl" navi="treeview.aspx?id=5" />
<product productname="Black Portoro" navi="treeview.aspx?id=6" />
<product productname="Blue Pearl" navi="treeview.aspx?id=7" />

</Category>
<Category Catname="Indian Granite">
 <product productname="Rosso Pistello " navi="treeview.aspx?id=30" />
<product productname="Rosso Verona " navi="treeview.aspx?id=31" />
<product productname="Shell Beige " navi="treeview.aspx?id=32" />
<product productname="White onyx " navi="treeview.aspx?id=33" />
<product productname="Antique Beige" navi="treeview.aspx?id=1" />
<product productname="YELLOW_big" navi="treeview.aspx?id=34" />
</Category>
<Category Catname="Iron Ore">
   <product productname="MYRA BEIZE" navi="treeview.aspx?id=22" />
<product productname="Noche Travertine " navi="treeview.aspx?id=23" />
<product productname="Perlato Sicilia " navi="treeview.aspx?id=24" />
<product productname="Red Alicante " navi="treeview.aspx?id=25" />
<product productname="Red Fire " navi="treeview.aspx?id=26" />
<product productname="Red Travertine " navi="treeview.aspx?id=27" />
<product productname="Rosso Atlanta " navi="treeview.aspx?id=28" />
<product productname="Rosso Lavente " navi="treeview.aspx?id=29" />
<product productname="Rosso Pistello " navi="treeview.aspx?id=30" />
<product productname="Rosso Verona " navi="treeview.aspx?id=31" />
<product productname="Shell Beige " navi="treeview.aspx?id=32" />
<product productname="White onyx " navi="treeview.aspx?id=33" />
<product productname="Antique Beige" navi="treeview.aspx?id=1" />
<product productname="YELLOW_big" navi="treeview.aspx?id=34" />
</Category>
</cat>


No comments:

Post a Comment