You are viewing a read-only archive of the Blogs.Harvard network. Learn more.

TreeView Example

June 30th, 2006

On the Page_Load, two drop downs and an Infragistics control object are initialized. Once the user has entered the search criteria in these objects and pressed the Search button, the btnSearch_Click event is fired. This event initializes the treeview object.

When the treeview is clicked, the TreeView1_TreeNodePopulate event is fired and the appropriate node is populated with one of three possible data trees (Practitioner, Category or Privilege).

When a leaf is clicked, the TreeView1_SelectedNodeChanged event is fired. This event, populates the DetailsView with data corresponding to the leaf that was clicked.


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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 Picis.Common.Functions.Attributes;
using Picis.Common.Enumerations.CredentialingManager;
using Picis.Common.Enumerations.Framework.Security;
using Picis.Web.Framework.Components.Core;
using Picis.Web.Framework.Components.Security;
using PWACMP = Picis.Web.Applications.CredentialingManager.Practitioner;
using PWACMD = Picis.Web.Applications.CredentialingManager.Dictionaries;
using Picis.Web.Applications.Common.Images;
using Picis.Web.Applications.UserPreferences;
using Picis.Web.Exceptions.CredentialingManager.Dictionaries;
using Picis.Common.Trace;

using Infragistics.WebUI.UltraWebGrid;

public partial class PrivilegeInquiry : ControlBase
{
#region Event Handlers

protected void Page_Load(object sender, EventArgs e)
{
string jsArray = “privInqControlIds”;

if (!this.IsPostBack)
{
ddlSearchType.Items.Add(new ListItem(“Practitioner”, “1”));
ddlSearchType.Items.Add(new ListItem(“Privilege”, “2”));
ddlSearchType.Items.Add(new ListItem(“Privilege Category”, “3”));
ddlSearchType.Items[0].Selected = true;

ddlSearchBy.Items.Add(new ListItem(“Mnemonic”, “1”));
ddlSearchBy.Items.Add(new ListItem(“Name”, “2”));
ddlSearchBy.Items.Add(new ListItem(“Description”, “3”));
ddlSearchBy.Items[0].Selected = true;

wcFacility.ClientSideEvents.InitializeCombo = “wcFacility_InitializeCombo”;
wcFacility.ClientSideEvents.EditKeyUp = “wcFacility_EditKeyUp”;
wcFacility.DataTextField = “instn_mnc”;
wcFacility.DataValueField = “instn_nbr”;
wcFacility.DataSource = PWACMD.FacilityEntry.GetAllFacilities(false);
wcFacility.DataBind();
wcFacility.Columns[0].Hidden = true; //hide urn column
wcFacility.Columns[2].Hidden = true; //hide active column
wcFacility.Columns[1].Header.Caption = “Mnemonic”;
wcFacility.Columns[3].Header.Caption = “Description”;
wcFacility.Columns[1].Width = Unit.Percentage(30);
wcFacility.Columns[3].Width = Unit.Percentage(70);

//get user id
string userid = ((PWAMembershipUser)Membership.GetUser()).PicisUserID;

//get default facility
Dictionary prefFac = UserPreference.GetUserPreferences(userid, ApplicationType.CredentialingManager, “default_facility”);

//set facility to default
if(prefFac.Count>0)
{
wcFacility.DataValue = Convert.ToInt32(prefFac[0]);
}

//get the grid control within the combo and set the cell click action to rowselect
foreach (Control c in wcFacility.Controls)
{
if (c.ID == “_Grid”)
{
UltraWebGrid g = (UltraWebGrid)c;
g.DisplayLayout.CellClickActionDefault = CellClickAction.RowSelect;
}
}

wcSearch.DataValueField = “URN”;
wcSearch.ClientSideEvents.EditKeyUp = “wcSearch_EditKeyUp”;
wcSearch.ClientSideEvents.InitializeCombo = “wcSearch_InitializeCombo”;
((UltraWebGrid)this.wcSearch.Controls[0]).DisplayLayout.ClientSideEvents.BeforeRowActivateHandler = “wcSearch_BeforeRowActivated”;
((UltraWebGrid)this.wcSearch.Controls[0]).DisplayLayout.AllowAddNewDefault = AllowAddNew.Yes;
Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
“/ig_common/20061/scripts/ig_webgrid_an.js”, “”, “/ig_common/20061/scripts/ig_webgrid_an.js”,
“ig_webgrid_an.js”, this.wcSearch, “ig_webgrid_an”);

btnSearch.Text = “Search”;
lblFacility.Text = “Facility:”;
lblFind.Text = “Find:”;
lblSearchType.Text = “Search For:”;
lblSearchBy.Text = “Search By:”;
lblCategoryMnemonic.Text = “Category Mnemonic:”;
lblCategoryDescription.Text = “Category Description:”;

tblPractitionerDetail.Visible = false;
tblPrivilegeCategoryDetail.Visible = false;
tblPrivilegeDetail.Visible = false;
rptResultsByPractitioner.Visible = false;
rptResultsByOther.Visible = false;

}

Page.ClientScript.RegisterArrayDeclaration(jsArray,
“\”” + ddlSearchBy.ClientID + “\”,” +
“\”” + ddlSearchType.ClientID + “\””);

AjaxPro.Utility.RegisterTypeForAjax(typeof(PrivilegeInquiry), this.Page);
}

protected void btnSearch_Click(object sender, EventArgs e)
{
//Create RootNode
TreeNode rootnode = new TreeNode(“By ” + wcSearch.DisplayValue.ToString(), inquiryType.ToString());
rootnode.PopulateOnDemand = true;
rootnode.SelectAction = TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(rootnode);
}

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
//Populate variables based on Node Depth
int table = -1;
bool populateOnDemand = false;
TreeNodeSelectAction action = TreeNodeSelectAction.None;
switch (e.Node.Depth)
{
case 0:
switch (e.Node.Value)
{
//Category
case “Category”:
//Populate Privilege Nodes
table = 1;
populateOnDemand = true;
action = TreeNodeSelectAction.Expand;
break;
//Privilege
case “Privilege”:
//Populate Practitioner Nodes
table = 2;
populateOnDemand = false;
action = TreeNodeSelectAction.Select;
break;
//Practitioner
case “Practitioner”:
//Populate Category Nodes
table = 0;
populateOnDemand = true;
action = TreeNodeSelectAction.Expand;
break;

default:
throw new NotImplementedException(“Selection type not supported.”);
}
break;

case 1:
//Category
if (e.Node.ValuePath.Contains(“Category/” + e.Node.Value))
{
//Populate Practitioner Nodes
table = 2;
populateOnDemand = false;
action = TreeNodeSelectAction.Select;
}
//Privilege
if (e.Node.ValuePath.Contains(“Privilege/” + e.Node.Value))
{
throw new NotImplementedException(“Privilege not supported at this Node Depth.”);
}
//Practitioner
if (e.Node.ValuePath.Contains(“Practitioner/” + e.Node.Value))
{
//Populate Privilege Nodes
table = 1;
populateOnDemand = false;
action = TreeNodeSelectAction.Select;
}
break;

default:
throw new NotImplementedException(“Node depth not supported.”);

}

//Get table for treeview branch
DataView dv = GetPrivilegeInquiryDataView(table);

//Populate TreeView ChildNodes
switch (table)
{
//Category
case 0:
foreach (DataRow row in dv.Table.Rows)
{
if ((e.Node.Value == (row[“cat_nbr”]).ToString()) || (e.Node.Depth == 0))
{
TreeNode newNode = new TreeNode(row[“cat_mnc”].ToString() + ” – ” + row[“cat_desc”].ToString(), row[“cat_nbr”].ToString());
newNode.PopulateOnDemand = populateOnDemand;
newNode.SelectAction = action;
e.Node.ChildNodes.Add(newNode);
}
}
break;
//Privilege
case 1:
foreach (DataRow row in dv.Table.Rows)
{
if ((e.Node.Value == (row[“cat_nbr”]).ToString()) || (e.Node.Depth == 0))
{
TreeNode newNode = new TreeNode(row[“priv_mnc”].ToString() + ” – ” + row[“priv_descr1”].ToString(), row[“priv_nbr”].ToString());
newNode.PopulateOnDemand = populateOnDemand;
newNode.SelectAction = action;
e.Node.ChildNodes.Add(newNode);
}
}
break;
//Practitioner
case 2:
foreach (DataRow row in dv.Table.Rows)
{
if ((e.Node.Value == (row[“priv_nbr”]).ToString()) || (e.Node.Depth == 0))
{
TreeNode newNode = new TreeNode(row[“dr_mnc”].ToString() + ” – ” + row[“dr_nm”].ToString(), row[“dr_nbr”].ToString());
newNode.PopulateOnDemand = populateOnDemand;
newNode.SelectAction = action;
e.Node.ChildNodes.Add(newNode);
}
}
break;

default:
throw new NotImplementedException(“Table not supported.”);

}

}
}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
string select;

//If Practitioner, filter by Privileges
if (TreeView1.SelectedNode.ValuePath.Contains(“Practitioner/”))
{
select = “priv_nbr = ” + TreeView1.SelectedNode.Value.ToString();
}
//If Privilege, filter by Practitioner
else
{
select = “dr_nbr = ” + TreeView1.SelectedNode.Value.ToString();
}

//Get DetailsView data from Table 2
DataView dv = GetPrivilegeInquiryDataView(2);
dv.RowFilter = select;

//Populate DetailsView
DetailsView1.DataSource = dv;
DetailsView1.DataBind();

}

#endregion

#region ControlBase Members

public override void InitUserControl()
{
Page.ClientScript.RegisterHiddenField(“nameText”, “Name”);
Page.ClientScript.RegisterHiddenField(“descriptionText”, “Description”);
}

#endregion

#region Public Methods

///
/// AJAX method to retrieve a DataTable consisting of information about the first
/// five entities to match the given search criteria.
///
///
/// DataTable is formatted as follows:
///
/// mnc as string
/// descr as string
/// urn as int
///
/// Search string.
/// Integer representation of the inquiry type:
/// 1 = Practitioner
/// 2 = Privilege
/// 3 = Privilege Category
///
/// Integer representation of the search type:
/// 1 = search by mnemonic
/// >1 = search by description or name
///
/// Integer id used to identify this search request on the client.
/// This id is returned to the client in the URN column of the last row of the table.
///
/// DataTable of five or less rows plus a row with the request id.
[AjaxPro.AjaxMethod]
public DataTable SearchLookup(string search, int inquiryType, int lookupType, int requestId)
{
DataTable dt;

bool byDesc = (lookupType == 1) ? false : true;

switch (inquiryType)
{
case 1:
dt = PWACMP.Privilege.SearchPractitioners(search, byDesc);
break;
case 2:
dt = PWACMP.Privilege.SearchPrivileges(search, byDesc);
break;
default:
dt = PWACMP.Privilege.SearchPrivilegeCategories(search, byDesc);
break;
}

//add request ID
DataRow dr = dt.NewRow();
dr[“urn”] = requestId;
dt.Rows.Add(dr);

return dt;
}

[AjaxPro.AjaxMethod]
public DataSet GetDetail(int urn, string detailType, int requestId)
{
DataSet ds = new DataSet();

DataTable rid = new DataTable();
rid.Columns.Add(“detail_type”, typeof(String));
rid.Columns.Add(“request_id”, typeof(Int32));
rid.Rows.Add(detailType, requestId);

ds.Tables.Add(rid);

switch (detailType)
{
case “P”:
DataTable dt = new DataTable();
dt.Columns.Add(“img_urn”, typeof(Int32));
dt.Columns.Add(“img_caption”, typeof(String));

List imgs = SecurityManagerImage.GetImages(
ApplicationType.CredentialingManager, 100, 100, urn.ToString());

if (imgs.Count == 1)
{
dt.Rows.Add(imgs[0].Urn, imgs[0].ShortDescription);
}

ds.Tables.Add(dt);

break;
case “R”:

break;
default:

break;
}

return ds;
}

#endregion

#region Protected Methods

protected DataView GetChildRelation(object dataItem, string relation)
{
DataRowView drv = dataItem as DataRowView;

if (drv != null)
{
return drv.CreateChildView(relation);
}
else
{
return null;
}
}

#endregion

#region Private Methods

private DataView GetPrivilegeInquiryDataView(int table)
{
//Get Search Crieria Values
int urn = Convert.ToInt32(wcSearch.DataValue);
int facUrn = Convert.ToInt32(wcFacility.DataValue);
PrivilegeInquiryType inquiryType;
switch (ddlSearchType.SelectedValue)
{
case “1”:
inquiryType = PrivilegeInquiryType.Practitioner;
break;
case “2”:
inquiryType = PrivilegeInquiryType.Privilege;
break;
case “3”:
inquiryType = PrivilegeInquiryType.Category;
break;
default:
throw new NotImplementedException(“Inquiry type not supported.”);
}

//Get PrivilegeInquiry DataTable based on Search Crieria Values
DataTable dt = new DataTable();
switch (table)
{
case 0:
dt = PWACMP.Privilege.PrivilegeInquiry(urn, facUrn, inquiryType).Tables[0];
break;
case 1:
dt = PWACMP.Privilege.PrivilegeInquiry(urn, facUrn, inquiryType).Tables[1];
break;
case 2:
dt = PWACMP.Privilege.PrivilegeInquiry(urn, facUrn, inquiryType).Tables[2];
break;
default:
throw new NotImplementedException(“Table not supported.”);
}

//Return DataTable as DataView
DataView dv = new DataView(dt);
return dv;

}

#endregion
}

Entry Filed under: C# and .NET

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>