How to access an ASP.Net TreeView node databound to a SiteMap – and remove it

You may have a situation where you are populating an ASP.Net TreeView control from a SiteMap xml file but don’t want certain nodes to be shown on the page.

The following C# code shows how to access the node/s in code behind when they are databound and remove specific nodes based on their Sitemap node title.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 protected void myTreeView_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
 {
     System.Web.UI.WebControls.TreeView tv = (System.Web.UI.WebControls.TreeView)sender;
     SiteMapNode mn = (SiteMapNode)e.Node.DataItem;
 
     System.Web.UI.WebControls.TreeNode nodeToRemove = tv.FindNode(mn.Title);
 
     if (mn.Title == "My Sitemap Node Title")
     {
         System.Web.UI.WebControls.TreeNode parent = e.Node.Parent;
         if (parent != null)
         {
              parent.ChildNodes.Remove(e.Node);
         }
      }
 }

The above code will search for all nodes with the title “My Sitemap Node Title” and remove them from the TreeView, meaning they will not be rendered to the page.

The same can be done with most item collections in the .Net Framework. Here’s another example, this time removing items from an ASP.Net Menu control:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void myMenu_MenuItemDataBound(object sender, MenuEventArgs e)
 {
     System.Web.UI.WebControls.Menu menu = (System.Web.UI.WebControls.Menu)sender;
     SiteMapNode mapNode = (SiteMapNode)e.Item.DataItem;
 
     System.Web.UI.WebControls.MenuItem itemToRemove = menu.FindItem(mapNode.Title);
 
     if (mapNode.Title == "My Sitemap Node Title")
     {
          System.Web.UI.WebControls.MenuItem parent = e.Item.Parent;
          if (parent != null)
          {
                parent.ChildItems.Remove(e.Item);
          }
      }
 }

Umbraco – Getting child nodes (.net)

using umbraco.NodeFactory; 
//umbraco.presentation.nodeFactory is now depricated;

protected void Page_Load(object sender, EventArgs e)
    	{
    		//If you just want the children of the current node use the following method
    		var currentNode = Node.GetCurrent();

    		//If you need a specific node based on ID use this method (where 123 = the desired node id)
    		var specificNode = new Node(123);

    		//To get the children as a Nodes collection use this method
    		var childNodes = specificNode.Children;

    		//Iterating over nodes collection example
    		foreach(var node in childNodes)
    		{
    			Response.Write(string.Format("{0}<br />", node.Name));
    		}

    		//To get the nodes as a datatable so you can use it for DataBinding use this method
    		var childNodesAsDataTable = node.ChildrenAsTable();

    	   //Databind example
    	   GridViewOnPage.DataSource = childNodesAsDataTable;
    	   GridViewOnPage.DataBind();
    	}

Hiding the PageSizeComboBox and ChangePageSizeLabel from a Telerik RadGrid

By default, there is no setting to hide the PageSizeComboBox or ChangePageSizeLabel from the pager control in a Telerik RadGrid control.  Instead, we must use the grid’s ItemDataBound event to handle the hiding of these items, like so:

1
2
3
4
5
6
7
8
9
10
11
12
protected void myRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridPagerItem)
{
GridPagerItem pager = (GridPagerItem)e.Item;
Label lbl = (Label)pager.FindControl("ChangePageSizeLabel");
lbl.Visible = false;
 
RadComboBox combo = (RadComboBox)pager.FindControl("PageSizeComboBox");
combo.Visible = false;
}
}

The above code checks to see if the item being data bound is of the type “GridPagerItem”. If it is, it then creates a pager item and casts a label from this pager item so that it’s “visible” attribute can be set to “false”, thus hiding it from the grid at run time.

The same is done for the combobox, only this time a RadComboBox is cast from the pager item instead, and then hidden too.

Using cookieless sessionState in ASP.Net

If your ASP.Net application is storing sessionState data and you want to avoid using cookies, this can be achieved by simply setting the sessionState cookieless parameter to “true”. This will create a unique id for the sessionState and ammend any application URLs by adding this id to them.

For example, if the URL of your application homepage normally looks like this

http://localhost/myapplication/default.aspx

Cookieless sessionState would ammend the URL to read:

http://localhost/myapplication/(S(somerandomID))/default.aspx

Setting this up is simple. It is merely a case of changing the cookieless value of the sessionState entry in the Web.config file of your application, like so:

<system.web>
     <sessionState mode="InProc" timeout="60" cookieless="true" />
</system.web>

The example above uses the InProcess sessionState mode (which ASP uses by default for performance), sets a session timeout of 60 minutes and enables cookieless sessionState. The beauty of this built in feature means that ASP.Net handles all sessionState cookieless features for you. All URLs in your application will be ammended with this sessionState ticket and sent with the queryString at every postback to the server – no other coding is required.

Please note that the state will be lost if the user url-rewrites any of the links within your application, but so long as the user keeps to the links within your site then state will be maintained and any data you have stored in the way of session variables, for example, will be available to the application.

Populating an ASP.Net drop down list using LINQ to SQL with C#

This example shows how to populate an ASP.Net drop down list control using LINQ to SQL with C#.  This example uses a stored procedure called from within the code behind  of the ASP.Net page. A sample method passes in a user ID which is then passed to the stored procedure.

Once you have created the datacontext diagram for your application, simply create an instance of it and populate the control as so:

protected void sampleMethod(int userId)
{
     using (var dc = new MyApplicationDataContext())
     {
          ddlMyDropDownList.DataSource = dc.SP_myStoredProc(userId);
          ddlMyDropDownList.DataTextField = "USER_NAME";
          ddlMyDropDownList.DataValueField = "USER_ID";
          ddlMyDropDownList.DataBind();
     }
}

You can also add an inline value to be shown in the drop down list and insert it at any point in the list at runtime, like so:

protected void sampleMethod(int userId)
{
     using (var dc = new MyApplicationDataContext())
     {
          ddlMyDropDownList.DataSource = dc.SP_myStoredProc(userId);
          ddlMyDropDownList.DataTextField = "USER_NAME";
          ddlMyDropDownList.DataValueField = "USER_ID";
          ddlMyDropDownList.DataBind();
          ListItem li = new ListItem("Inline Value", "78");
          ddlMyDropDownList.Items.Insert(0, li);
     }
}

The above code creates a list item and assigns some text and a value to it. The last line then inserts the new item into the drop down list at the first index in the list (i.e. the top).