Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can dynamically bind asp.net treeview control from database with image icon
Posted

1 solution

The following will be your solution:

aspx page code
-------------
<table class="tvSiteStyle" cellpadding="0" cellspacing="0" style="width:100%;" >
<tr>
<td style="vertical-align: top; text-align: left;" class="SiteMenuBackGround">
<asp:TreeView ID="tvSite" runat="server" ExpandDepth="0" EnableTheming="True" >
<SelectedNodeStyle BorderColor="Silver" CssClass="SiteMenu1" />
<ParentNodeStyle CssClass="SiteMenu1 " />
<RootNodeStyle CssClass="SiteMenu1 " />
<LeafNodeStyle CssClass="SiteMenu1" />
<NodeStyle CssClass="SiteMenu1 " />
</asp:TreeView>
</td>
</tr>
</table>

cs code
==========
using System;
using System.Data;
using YOS.BL.Interface.Enums;
using YOS.UTL;
public enum E_PAGEACCESSTYPE : int
{
ALL=0,
PUBLIC=1,
PRIVATE=2
}
public partial class wucSiteMenu : System.Web.UI.UserControl
{
private int cIntUserLevel=100;
public int intUserLevel
{

get { return cIntUserLevel; }

}

private TreeNode tnGetNodeByValue(TreeNode tnNode,string strValue)
{
TreeNode tnTemp=null;
if (tnNode.Value == strValue)
{
tnTemp= tnNode;
}
else
{
foreach (TreeNode tnChildNode in tnNode.ChildNodes)
{
tnTemp = tnGetNodeByValue(tnChildNode, strValue);
if (tnTemp != null) break;
}
}
return tnTemp;

}


public void FillSiteTree(DataRow[] drSite)
{
TreeNode tnTemp;
TreeNode tnParent;

for(int i=0;i<drsite.length;i++)>
{
string strTemp;
tnTemp = new TreeNode();
tnTemp.Text = " " + DataHandler.strHandleDbValue(drSite[i]["Name"]);
tnTemp.Value = DataHandler.strHandleDbValue(drSite[i]["ItemID"]);
strTemp = DataHandler.strHandleDbValue(drSite[i]["URL"]);
if (strTemp.IndexOf("?")< 1)
{
strTemp = strTemp + "?";
}

if (Session["UserID"] == null)
{

tnTemp.NavigateUrl = strTemp + "&LPage=default.aspx";
}
else
{
if (!strTemp.Contains("PID="))
{
strTemp = strTemp + "&PID=" + tnTemp.Value;
}
strTemp = strTemp.Replace("[MEMBERID]", Convert.ToString(Session["MemberID"])).Replace("[USERID]", Convert.ToString(Session["UserID"]));
tnTemp.NavigateUrl = strTemp + "&LPage=UserHome.aspx";
}

tnTemp.ImageUrl = DataHandler.strHandleDbValue(drSite[i]["ImageSmallURL"]);
tnTemp.ToolTip = DataHandler.strHandleDbValue(drSite[i]["Description"]);
if (Convert.ToInt32(drSite[i]["ParentItemID"]) == 0)
{
tvSite.Nodes.Add(tnTemp);
}
else
{
foreach (TreeNode tnItem in tvSite.Nodes)
{
tnParent = tnGetNodeByValue(tnItem, DataHandler.strHandleDbValue(drSite[i]["ParentItemID"]));
if (tnParent != null)
{
tnParent.ChildNodes.Add(tnTemp);
}
}
}
}
}

private DataRow[] drGetSite()
{
YOS.BL.Security secSite=new YOS.BL.Security();

int intUserLevel=0;
string strUserID=null;
if (Session["UserID"]!=null)
{
strUserID=Convert.ToString( Session["UserID"]);
}
if (Session["UserLevel"]!=null)
{
intUserLevel=Convert.ToInt32( Session["UserLevel"]);
}
return secSite.drGetSiteLinks(intUserLevel, strUserID,"");

}
private void ExpandNodeLine(TreeNode tnNode)
{
if (tnNode == null) return;
tnNode.Expand();
tnNode.Text = "" + tnNode.Text + "";
ExpandNodeLine(tnNode.Parent);
}

public void ClearCache()
{
foreach (Object entry in this.Cache)
{
this.Cache.Remove((string)entry.ToString());
}
}

public void FillSiteMenu()
{
if (cIntUserLevel != 100)
{
if (Session["UserLevel"] == null)
{
if (cIntUserLevel == 0)
{
return;
}
}
else
{
if (cIntUserLevel == Convert.ToInt32(Session["UserLevel"]))
{
return;
}

}
}

FillSiteTree(drGetSite());
if (Request.QueryString["PID"] != null)
{
TreeNode tnNode;
foreach (TreeNode tnItem in tvSite.Nodes)
{
tnNode = tnGetNodeByValue(tnItem, Convert.ToString(Request.QueryString["PID"]));

if (tnNode != null)
{
tnNode.Text = "" + tnNode.Text + "";
tnNode.Selected = true;
tnNode.Expand();
ExpandNodeLine(tnNode.Parent);
break;
}
}


}
if (Session["UserLevel"] != null)
{
cIntUserLevel = Convert.ToInt32(Session["UserLevel"]);
}
else
{
cIntUserLevel = 0;
}
}
protected void Page_Load(object sender, EventArgs e)
{
try
{



if (Request.QueryString["Con"] == "false" || IsPostBack)
{

return;
}

if (!IsPostBack)
{

string ss = Request.RawUrl;
FillSiteMenu();



}

}
catch (Exception ex)
{
cIntUserLevel = 100;
ClearCache();
Response.Redirect("Message.aspx?Con=false&Ok=OK&Title=Connection Error&Desc=Connection with data source cannot be established&Img=1&LPage=default.aspx");
}


}
void Page_Error(Object sender, EventArgs args)
{
Response.Write("Error:\n");
Exception e = Server.GetLastError();
Trace.Write("Message", e.Message);
Trace.Write("Source", e.Source);
Trace.Write("Stack Trace", e.StackTrace);
Response.Write("Sorry, an error was encountered");
Context.ClearError();
}

}

BL layer code
----------------
public DataRow[] drGetSiteLinks(int intUserLevel, string strUserID,string strFilter)
{
YOS.DAL.DSecurity dsecSite = new YOS.DAL.DSecurity();
return dsecSite.drGetSiteLinks(intUserLevel, strUserID, strFilter);
}

DAL Layer code
---------------
public DataRow[] drGetSiteLinks(int intUserLevel,string strUserID,string strFilter)
{
DataTable dtCache = new DataTable();
if (HttpContext.Current.Cache["SiteLinks"] == null)
{

dtCache = YOS.UTL.Connection.dtGetDataTableBySQL("Select * from tbl_Sec_Site where Status=1 order by ItemOrder");

HttpContext.Current.Cache.Add("SiteLinks", dtCache,null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
else
{
dtCache = (DataTable)HttpContext.Current.Cache["SiteLinks"];

}

string strTemp = "AccessType in ('A','";
if (strUserID != null && strUserID != "")
{
strTemp = strTemp + "I";

}
else
{
strTemp = strTemp + "E";

}
strTemp = strTemp + "')";


if (intUserLevel != 0)
{

strTemp = strTemp + " and ";
switch ((int)intUserLevel)
{
case (int)E_USERLEVEL.SYSTEM:
{
strTemp = strTemp + " Sysadmin=1";
break;
}
case (int)E_USERLEVEL.TOPLEVEL:
{
strTemp = strTemp + " localadmin=1";
break;
}
case (int)E_USERLEVEL.MIDLEVEL:
{
strTemp = strTemp + " midadmin=1";
break;
}
case (int)E_USERLEVEL.OPERATION:
{
strTemp = strTemp + " operationuser=1";
break;
}

case (int)E_USERLEVEL.ROOTUSER:
{
strTemp = strTemp + " rootuser=1";
break;
}
}

}

if (strFilter != "" && strFilter != null)
{
strTemp += " and " + strFilter;
}

return dtCache.Select(strTemp, "ItemOrder");


}

--------------
public static DataTable dtGetDataTableBySQL(string strSQL)
{

try
{


string ConString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//string ConString = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("ConnectionString");

DataTable dtTable = new DataTable();
//DataSet ds = new DataSet();
using (SqlConnection sqlCon = new SqlConnection(ConString))
{

using (SqlDataAdapter sdaDataAdapter = new SqlDataAdapter(strSQL, sqlCon))
{

sdaDataAdapter.Fill(dtTable);

//sdaDataAdapter.Fill(ds, "DS_Table");
}

}

return dtTable;
//return ds.Tables["DS_Table"];

}
catch (Exception ex)
{
throw ex;
}


}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900