Guitar Lover

Guitar Lover

  • NA
  • 3
  • 745

How to add alphabets to the child nodes in a tree view?

Aug 5 2016 2:29 AM
Design-
<asp:TreeView ID="myTreeView" runat="server" ImageSet="Arrows" PathSeparator="|" ShowCheckBoxes="All" >
<HoverNodeStyle Font-Underline="False" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="False" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<DataBindings>
<asp:TreeNodeBinding DataMember="Category" ValueField="ID" TextField="Name">
</asp:TreeNodeBinding>
<asp:TreeNodeBinding DataMember="Description" ValueField="Value" TextField="Value">
</asp:TreeNodeBinding>
</DataBindings>
</asp:TreeView>
 
 
Codebehind-
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.Data.OleDb;
using FCS.Model;
using FCS.BLL;
using System.Linq;
using ServiceLayer;
/// <summary>
///
/// </summary>
public partial class Admin_AddEditRole : System.Web.UI.Page
{
#region Variables & Properties
FCS.Common.Message messageObj = new FCS.Common.Message();
FCS.BLL.RoleManagerBLL objBLL = new FCS.BLL.RoleManagerBLL();
RoleInfo ObjRI = new RoleInfo();
string _xmlPath;
string _message;
public string _loginIdSess;
string strOperation = "";
public int RoleID
{
get
{
if (ViewState["RoleID"] != null)
{
return Convert.ToInt32(ViewState["RoleID"]);
}
else
return -1;
}
set
{
ViewState["RoleID"] = value;
}
}
#endregion Variables & Properties
#region Events
protected void Page_Init(object sender, EventArgs e)
{
txtNewRoleName = (TextBox)AccordionPane1.FindControl("txtNewRoleName");
DropDownRoleName = (DropDownList)AccordionPane1.FindControl("DropDownRoleName");
txtRoleDesc = (TextBox)AccordionPane1.FindControl("txtRoleDesc");
ChbSelectAll = (CheckBox)AccordionPane2.FindControl("ChbSelectAll");
ChbExpandCollapse = (CheckBox)AccordionPane2.FindControl("ChbExpandCollapse");
myTreeView = (TreeView)AccordionPane2.FindControl("myTreeView");
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
ClientScript.RegisterHiddenField("__EVENTTARGET", "imgBtnSave");
//ClientScript.RegisterHiddenField("__EVENTTARGET", "imgBtnSave");
_xmlPath = ConfigurationManager.AppSettings["XmlPath"].ToString();
_xmlPath = Server.MapPath(_xmlPath).ToString();
_loginIdSess = Convert.ToString(Session["MemberID"]);
string whichFrm = (string)Session["WhichForm"];
myTreeView.Attributes.Add("onclick", "javascript:OnCheckBoxCheckChanged(event);");
ChbSelectAll.Attributes.Add("onclick", "javascript:SelectAllCheckboxes(event);");
lblError.Visible = false;
lblMessage.Visible = false;
if (!IsPostBack)
{
// Bind 'Copy forn Roles' dropdown
DataSet Ds_Roles = objBLL.GetRoleBLL(_loginIdSess + "|getUserChildRoles|" + Session["RoleId"].ToString().Trim());
DropDownRoleName.DataSource = Ds_Roles.Tables[0];
DropDownRoleName.DataTextField = "ROLE_CODE";
DropDownRoleName.DataValueField = "ROLE_ID";
DropDownRoleName.DataBind();
DropDownRoleName.Items.Insert(0, "--SelectRole--");
if (Request.QueryString["Role"] != null)
{
NotificationOp objOp = new NotificationOp();
string strRoleName = Request.QueryString["Role"].ToString();
RoleID = objOp.GetRoleID(strRoleName, Convert.ToString(Session["ORG_ID"]));
txtNewRoleName.Text = strRoleName;
txtNewRoleName.Enabled = false;
txtRoleDesc.Text = Request.QueryString["Desc"].ToString();
DataSet Role = objBLL.GetRoleBLL(_loginIdSess + "|getOpsUsrRole|" + strRoleName);
ChkBGlobalRoles.Checked = (Role.Tables[0].Rows[0]["IS_DEFAULT"].ToString() == "X") ? true : false;
if (Convert.ToInt32(objBLL.GetRoleBLL(_loginIdSess + "|IsGRoleMap|" + strRoleName).Tables[0].Rows[0][0].ToString()) > 0)
ChkBGlobalRoles.Enabled = false;
else
ChkBGlobalRoles.Enabled = true;
ViewState["previouspage"] = "../Admin/ViewRole.aspx";
}
else
{
ViewState["previouspage"] = Request.UrlReferrer.ToString();
txtNewRoleName.Text = "";
txtNewRoleName.Enabled = true;
}
DataSet roles = CreateTree(objBLL.GetRoleBLL(_loginIdSess + "|vwMenu"));
ViewRole(txtNewRoleName.Text.Trim());
GetNotificationTypes();
CheckNotificationsSelected(RoleID.ToString());
Ds_Roles.Dispose();
}
if (Session["strRole"].ToString() != messageObj.GetMessage("//Admin/SuperAdmin", _xmlPath))
{
ChkBGlobalRoles.Visible = false;
}
else
{
ChkBGlobalRoles.Visible = true;
}
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->Page_Load: " + ex.Message.ToString(), _loginIdSess, 1);
}
finally
{
try
{
EnableDisableControls();
}
catch (System.Exception ex)
{
messageObj.WriteLog("frmCTCTDetails1->Page_Load: " + ex.Message.ToString(), _loginIdSess, 1);
DisableControls();
}
}
}
protected void ImgBtnSave_Click(object sender, ImageClickEventArgs e)
{
try
{
if (Page.IsValid && ValidateRole())
{
if (txtNewRoleName.Enabled == true)
{
strOperation = "RoleAdd";
}
else
{
strOperation = "RoleModify";
}
ObjRI.Privileges = getSelectedRole(myTreeView);
ObjRI.RoleName = txtNewRoleName.Text.Trim().ToUpper();
ObjRI.RoleDesc = txtRoleDesc.Text.Trim();
ObjRI.Parent_role = Session["RoleId"].ToString().Trim();
ObjRI.UserName = _loginIdSess;
ObjRI.IsGloble = (ChkBGlobalRoles.Checked == true) ? "X" : "N";
if (txtNewRoleName.Text.Trim() == string.Empty)
{
_message = messageObj.GetMessage("//Admin/RoleManagerRoleName", _xmlPath);
lblError.Text = _message;
lblError.Visible = true;
return;
}
if (txtRoleDesc.Text.Trim() == string.Empty)
{
_message = messageObj.GetMessage("//Admin/RoleManagerRoleDesc", _xmlPath);
lblError.Text = _message;
lblError.Visible = true;
return;
}
if (txtNewRoleName.Enabled == true)
{
DataSet Ds_User_Role = objBLL.GetRoleBLL(Convert.ToString(Session["ORG_ID"]) + "|getUserRole");//USR_ROLE
DataSet Ds_Roles_Count = objBLL.GetRoleBLL(Convert.ToString(Session["ORG_ID"]) + "|getRolesCount|" + Ds_User_Role.Tables[0].Rows[0][0].ToString());
if (objBLL.GetRoleBLL(Convert.ToString(Session["ORG_ID"]) + "|getUserType|").Tables[0].Rows.Count > 0)
{
if (Ds_Roles_Count.Tables[0].Rows[0][0].ToString() != "1" && ChkBGlobalRoles.Visible == false)
{
_message = messageObj.GetMessage("//Admin/RoleManagerRoleLimit", _xmlPath);
lblError.Text = _message;
lblError.Visible = true;
return;
}
}
}
string output = objBLL.SaveRoleBLL(ObjRI, strOperation);
if (output != "1")
{
if (strOperation == "RoleModify")
{
_message = messageObj.GetMessage("//Admin/RoleManagerModificationFail", _xmlPath);
}
else
{
_message = messageObj.GetMessage("//Admin/RoleManagerCreationFail", _xmlPath);
}
messageObj.WriteLog("Admin_AddEditRole->imgBtnSave: " + _message, _loginIdSess, 1);
lblError.Text = _message;
lblError.Visible = true;
}
else
{
string remoteUrl = System.Configuration.ConfigurationManager.AppSettings["SessionManager"];
// Create an instance of the remote object
Remoting.RemoteInterface remoteObj = (Remoting.RemoteInterface)Activator.GetObject(typeof(Remoting.RemoteInterface), remoteUrl);
string roleID = objBLL.GetRoleBLL(ObjRI.UserName + "|getRoleID|" + ObjRI.RoleName).Tables[0].Rows[0][0].ToString();
if (strOperation == "RoleModify")
{
Session["strOptType"] = "U";
if (remoteObj.DelRoleId(roleID))
{
if (!remoteObj.AddRoleId(roleID))
{
messageObj.WriteLog("Admin_AddEditRole->ImgBtnSave_Click: Remoting AddRoleId Failed ", _loginIdSess, 0);
}
}
else
{
messageObj.WriteLog("Admin_AddEditRole->ImgBtnSave_Click: Remoting DelRoleId Failed", _loginIdSess, 0);
}
}
else
{
Session["strOptType"] = "I";
if (!remoteObj.AddRoleId(roleID))
{
messageObj.WriteLog("Admin_AddEditRole->ImgBtnSave_Click: Remoting AddRoleId Failed", _loginIdSess, 0);
}
}
SaveNotificationPermissions(roleID);
Session["WhichForm"] = "AddEditRole";
Response.Redirect("../Admin/frmAdminSuccess.aspx");
}
}
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->ImgBtnSave_Click: " + ex.Message.ToString(), _loginIdSess, 1);
}
}
protected void ImgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
strOperation = "RoleAdd";
}
protected void ImgBtnEdit_Click(object sender, ImageClickEventArgs e)
{
ViewRole(txtNewRoleName.Text.Trim());
}
protected void ImgBtnBack_Click(object sender, ImageClickEventArgs e)
{
try
{
string previouspage = ViewState["previouspage"].ToString();
Response.Redirect(previouspage);
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->ImgBtnBack_Click: " + ex.Message.ToString(), _loginIdSess, 1);
}
}
protected void DropDownRoleNm_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void DropDownRoleName_OnSelectedIndexChanged(object sender, EventArgs e)
{
ViewRole(DropDownRoleName.SelectedItem.Text.Trim());
}
protected void txtNewRoleName_OnTextChanged(object sender, EventArgs e)
{
lblError.Text = "";
lblRoleName.Text = "";
if (txtNewRoleName.Text.ToString().Trim().Length < 5)
{
lblRoleName.Text = "Enter minimum 5 character.";
txtNewRoleName.Text = "";
txtNewRoleName.Focus();
}
else if (objBLL.GetRoleBLL(_loginIdSess + "|getGlobRoleID|" + txtNewRoleName.Text.ToString().ToUpper()).Tables[0].Rows.Count > 0)
{
lblRoleName.Text = "Role already exits";
txtNewRoleName.Text = "";
txtNewRoleName.Focus();
}
else
{
txtRoleDesc.Focus();
}
}
#endregion Events
#region Methods
public string getSelectedRole(TreeView trRolesView)
{
try
{
string QueryString = "";
foreach (TreeNode node in trRolesView.CheckedNodes)
{
QueryString = QueryString + node.Value + "|";
}
QueryString = QueryString.Remove(QueryString.Length - 1);
return QueryString;
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->getSelectedRole: " + ex.Message.ToString(), _loginIdSess, 1);
return "";
}
}
public DataSet CreateTree(DataSet Dsrole)
{
TreeNode parentnode = new TreeNode();
TreeNode possparentnode = new TreeNode();
TreeNode node = new TreeNode();
try
{
if (Dsrole.Tables[0].Rows.Count > 0)
{
DataTable dtrole = Dsrole.Tables[0];
int maxlevel = Convert.ToInt32(dtrole.Compute("max(ACTIVITY_LEVEL)", ""));
DataTable dtPrivileges = objBLL.GetRoleBLL(Convert.ToString(Session["ORG_ID"]) + "|getPrivileges").Tables[0];
foreach (DataRow dr in Dsrole.Tables[0].Rows)
{
if (Convert.ToInt32(dr["PARENT_ACT_ID"].ToString().Trim()) == 0)
{
int i = Convert.ToInt32(dr["ACTIVITY_LEVEL"].ToString().Trim());
int parent = Convert.ToInt32(dr["ACTIVITY_ID"].ToString().Trim());
parentnode = null;
parentnode = new TreeNode();
parentnode.Text = dr["ACTIVITY_NAME"].ToString().Trim();
parentnode.Value = dr["ACTIVITY_ID"].ToString().Trim() + "-1";
parentnode.ToolTip = dr["ACTIVITY_DESC"].ToString().Trim();
TreeNode PNode = new TreeNode(dr["ACTIVITY_NAME"].ToString().Trim(), dr["ACTIVITY_ID"].ToString().Trim() + "-1");
myTreeView.Nodes.Add(PNode);
//Check no of levels for the parent node
var activitylevel = from p in dtrole.AsEnumerable()
where Convert.ToInt32(p["PARENT_ACT_ID"].ToString()) == Convert.ToInt32(parent)
&& Convert.ToInt32(p["ACTIVITY_LEVEL"].ToString()) == i + 1
orderby p["ACTIVITY_LEVEL"] descending
select p;
//PNode.ChildNodes.Add(new TreeNode("Child No : " , j.ToString()));
//useNo = randomNo.Next(2, 10);
if (activitylevel.Count() > 0)
{
int levelcnt = Convert.ToInt32(activitylevel.First()["ACTIVITY_LEVEL"].ToString());
for (int cnt = 1; cnt <= Convert.ToInt32(levelcnt); cnt++)
{
//AddChildNode(parentnode, parent, _menudt, i + 1, ref userIndex, ref UserPageID);
AddChildNode(PNode, parent, dtrole, dtPrivileges, i + 1);
AddChildNodeAccess(PNode, parent, dr, dtPrivileges, i + 1);
i++;
}
}
else
{
}
}
}
}
return Dsrole;
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->CreateTree: " + ex.Message.ToString(), _loginIdSess, 1);
return Dsrole;
}
}
private static void AddChildNode(TreeNode parentnode, int parent, DataTable dtrole, DataTable dtPrivileges, int level)
{
try
{
TreeNode possparentnode = new TreeNode();
TreeNode node = new TreeNode();
int possparent = 0;
var menus = from p in dtrole.AsEnumerable()
where Convert.ToInt32(p["PARENT_ACT_ID"].ToString()) == Convert.ToInt32(parent)
&& Convert.ToInt32(p["ACTIVITY_LEVEL"].ToString()) == level
select p;
foreach (var menu in menus)
{
possparent = Convert.ToInt32(menu["ACTIVITY_ID"].ToString().Trim());
possparentnode = null;
possparentnode = new TreeNode();
possparentnode.Text = menu["ACTIVITY_NAME"].ToString();
possparentnode.Value = menu["ACTIVITY_ID"].ToString() + "-1";
possparentnode.ToolTip = menu["ACTIVITY_DESC"].ToString();
//Update the UserPageID
//UserPageID[userIndex, 0] = menu["ACTIVITY_CODE"].ToString().Trim();
//UserPageID[userIndex, 1] = menu["ACTIVITY_URL"].ToString().Trim();
//UserPageID[userIndex, 2] = menu["PARENT_ACT_ID"].ToString().Trim();
//userIndex++;
parentnode.ChildNodes.Add(possparentnode);
parentnode.Selected = false;
if (possparent > 0)
{
var menusChild = from p in dtrole.AsEnumerable()
where Convert.ToInt32(p["PARENT_ACT_ID"].ToString()) == Convert.ToInt32(possparent)
&& Convert.ToInt32(p["ACTIVITY_LEVEL"].ToString()) == level + 1
select p;
if (menusChild.Count() > 0)
{
//AddChildNode(possparentnode, possparent, _menudt, level + 1, ref userIndex, ref UserPageID);
AddChildNode(possparentnode, possparent, dtrole, dtPrivileges, level + 1);
}
AddChildNodeAccess(possparentnode, possparent, menu, dtPrivileges, level + 1);
}
}
//return possparent;
}
catch (System.Exception ex)
{
throw ex;
}
}
private static void AddChildNodeAccess(TreeNode parentnode, int parent, DataRow dr, DataTable dtPrivileges, int level)
{
try
{
TreeNode possparentnode = new TreeNode();
TreeNode node = new TreeNode();
int possparent = 0;
string privileges = dr["PRIVILEGE_IDS"].ToString();
foreach (DataRow menu in dtPrivileges.Rows)
{
if (privileges.Contains(menu["PM_PRIVILEGE_ID"].ToString()))
{
possparent = Convert.ToInt32(dr["ACTIVITY_ID"].ToString().Trim());
possparentnode = null;
possparentnode = new TreeNode();
possparentnode.Text = menu["PM_PRIVILEGE_DESC"].ToString();
possparentnode.Value = dr["ACTIVITY_ID"].ToString() + "-" + menu["PM_PRIVILEGE_ID"].ToString();
possparentnode.ToolTip = dr["ACTIVITY_DESC"].ToString();
parentnode.ChildNodes.Add(possparentnode);
parentnode.Selected = false;
}
}
}
catch (System.Exception ex)
{
throw ex;
}
}
public void ViewRole(string strRoleName)
{
try
{
DataSet DS = objBLL.GetRoleBLL(_loginIdSess + "|getSelectedRole|" + strRoleName);
strOperation = "RoleModify";
string privileges = "";
for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
{
string[] privs = DS.Tables[0].Rows[i]["PRIVILEGE_IDS"].ToString().Split(',');
for (int j = 0; j < privs.Length; j++)
{
privileges = privileges + DS.Tables[0].Rows[i]["ACTIVITY_ID"].ToString() + "-" + privs[j].ToString() + "|";
}
}
if (privileges.Length > 0)
privileges = privileges.Remove(privileges.Length - 1);
foreach (TreeNode nodes in myTreeView.Nodes)
{
if (nodes.Value != null)
{
string[] privilegesList = privileges.Split('|');
bool privilegesStatus = false;
for (int i = 0; i < privilegesList.Length; i++)
{
if (privilegesList[i].ToString() == nodes.Value.ToString())
{
privilegesStatus = true;
}
}
if (privilegesStatus)
{
nodes.Checked = true;
Selectnodes(nodes, privileges);
}
else
{
nodes.Checked = false;
}
}
myTreeView.CollapseAll();
}
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->ViewRole: " + ex.Message.ToString(), _loginIdSess, 1);
}
}
public Boolean ValidateRole()
{
bool flag = true;
if (txtNewRoleName.Text.Trim() == string.Empty)
{
flag = false;
_message = "Please Enter Role Name";
}
if (flag == false)
{
messageObj.WriteLog("Admin_AddEditRole->imgBtnSave: " + _message, _loginIdSess, 1);
lblError.Text = _message;
lblError.Visible = true;
}
return flag;
}
public void Selectnodes(TreeNode nodes, string privileges)
{
try
{
if ((privileges.ToString().Split('|')).Contains(nodes.Value))// (privileges.Contains(nodes.Value))
{
nodes.Checked = true;
foreach (TreeNode Childnode in nodes.ChildNodes)
{
Selectnodes(Childnode, privileges);
}
}
}
catch (System.Exception ex)
{
messageObj.WriteLog("Admin_AddEditRole->Selectnodes: " + ex.Message.ToString(), _loginIdSess, 1);
}
}
#endregion Methods
# region AccessControl
public void EnableDisableControls()
{
FCS.BLL.CommonBLLOps dynobj = new FCS.BLL.CommonBLLOps();
string remoteUrl = System.Configuration.ConfigurationManager.AppSettings["SessionManager"];
DisableControls();
string activityId = dynobj.GetPrivileges(Session["RoleId"].ToString(), remoteUrl, Request.Url.AbsolutePath);
if (dynobj.Authenticatewithdnld(activityId.ToString(), "ADD", remoteUrl))
{
// Enable All Controls For ADD
ImageBtnSave.Visible = true;
}
if (dynobj.Authenticatewithdnld(activityId.ToString(), "EDIT", remoteUrl))
{
// Enable All Controls For EDIT
if (dynobj.Authenticatewithdnld(activityId.ToString(), "CHECKER", remoteUrl))
{
// Enable All Controls For CHECKER
}
// ONLY TO OTHER PAGE LINK REFERENCE REQUIRED
//string activityIdPageLink = dynobj.GetPrivileges(Session["RoleId"].ToString(), "Other Page Url ");
//if (dynobj.Authenticatewithdnld(activityIdPageLink.ToString(), "VIEW", remoteUrl))
//{
// // Enable All Controls For OTHER PAGE
//}
}
}
public void DisableControls()
{
// Disable All Controls Not Required for View Only
ImageBtnSave.Visible = false;
}
# endregion
#region Notification
private void GetNotificationTypes()
{
NotificationOp objOp = new NotificationOp();
DataTable dtNotTypes = objOp.GetNotificationTypes(Session["RoleId"].ToString().Trim());
chklstNotification.DataSource = dtNotTypes;
chklstNotification.DataValueField = "NT_ID";
chklstNotification.DataTextField = "NT_NAME";
chklstNotification.DataBind();
objOp.Dispose();
}
private void CheckNotificationsSelected(string strRoleID)
{
NotificationOp objOp = new NotificationOp();
DataTable dtNotTypes = objOp.GetNotificationTypes(strRoleID);
if (dtNotTypes != null && dtNotTypes.Rows.Count > 0)
{
foreach (DataRow dr in dtNotTypes.Rows)
{
foreach (ListItem boxItem in chklstNotification.Items)
{
if (Convert.ToString(dr["NT_ID"]) == boxItem.Value)
{
boxItem.Selected = true;
}
}
}
}
objOp.Dispose();
}
private string SaveNotificationPermissions(string strRoleID)
{
string output = string.Empty;
NotificationOp objOp = new NotificationOp();
ArrayList ntID = new ArrayList();
foreach (ListItem boxItem in chklstNotification.Items)
{
if (boxItem.Selected)
{
ntID.Add((string)boxItem.Value);
}
}
output = objOp.SaveNotificationIDForRole(strRoleID, ntID.ToArray(typeof(string)), Session["RoleId"].ToString().Trim());
objOp.Dispose();
return output;
}
private int GetRoleID(DataSet dsRoles, string RoleName)
{
int iValue = -1;
if (dsRoles.Tables[0].Select("ROLE_CODE='" + RoleName + "'", "").Count() > 0)
{
DataTable dt = dsRoles.Tables[0].Select("ROLE_CODE='" + RoleName + "'", "").CopyToDataTable();
iValue = Convert.ToInt32(dt.Rows[0]["ROLE_ID"]);
}
return iValue;
}
#endregion
}
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
yourTreeView.BeginUpdate();
foreach (var ch in alphabet)
{
var key = ch.ToString();
yourTreeView.Nodes.Add(key, key);
}
yourTreeView.Nodes.Add("?", "Other");
// access the db table...
while (reader.Read())
{
var name = reader("YourNameColumn").ToString().Trim();
var key = name.Substring(0, 1).ToUpper();
if (!alphabet.Contains(key))
{
key = "?";
}
yourTreeView.Nodes[key].Nodes.Add(name);
}
yourTreeView.EndUpdate();
 
 

Answers (2)