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);
          }
      }
 }

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.

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).