May 18 2006

Looping Through Page Controls

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 11:41

A very common question on the ASP.NET forums, is how to loop through the page controls, and find dynamically my control!

Here is the solution, its R E C U R S I O N !!

A very simple example, that loops through the page controls, and once it finds a Panel, it prints out, I am a Panel

 

    public void ShowPanels(Control p_Control)
    {
        try
        {
            // If control found is of type Panel
            if (p_Control.GetType().FullName.Equals("System.Web.UI.WebControls.Panel"))
            {
                                Response.Write ("I am a Panel!");
            }

            // Looping through all controls
            foreach (Control _control in p_Control.Controls)
            {
               ShowPanels(_control);
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.ToString());
        }
    }


Hope this helps,

Regards

Tags: ,

May 18 2006

PopulateNodesFromClient Property in TreeView - ASP.NET 2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 11:31

I have noticed today that, when you have PopulateOnDemand, and PopulateNodesFromClient both are true, then there is no way to access the Parent Node of the Current Node in your code!

I have been building a very complicated TreeView System in one of the projects working on, and I have really very complicated requirements for that Tree.

A workaround I came up with, whenever I see that the ParentNode is to be used by the children node, I add the Current Node Text and Value as the Value of the Children Node.

The key point, to let you know about this fact, and to help you find workarounds when you face such a need!

Hope this helps,

Regards

Tags:

May 12 2006

MasterPage For Each Folder

Category: ASP.NET 2.0 - GeneralBil@l @ 20:45

With ASP.NET 2.0 MasterPages, you can apply MasterPages per folder, how is that done?

<configuration>
  <location path="MyArea">
    <system.web>
      <pages masterPageFile="~/App_MasterPages/MyAreaMaster.master" />
    </system.web>
  </location>
</configuration>

So this way, you have mentioned that folder named MyArea, should have all its pages, the MasterPage called MyAreaMaster.master.

Hope this helps,

Regards

Tags:

May 12 2006

String Enumeration in C#

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 12:12

I have always had the need to use String Enumerations. What do I mean by that?

Suppose we have this Enum Type:

    public enum StringNames
    {
        Bilal,
        Wessam,
        Elsa,
        Samer,
        Tarek,
        Farah,
        Firas,
        Mohammad
    }

In my code, I would like to have something as:

string _Name = "Bilal";
switch (_Name)
{    
        case StringNames.Bilal:
                break;    
        case StringNames.Wessam:           
                break;     
        case StringNames.Elsa:           
                break;      
        .......
}

Well the above doesn't work as it is. How to make it work?

There is a method called Enum.Parse, there are two overloads, we will be using the one with the IgnoreCase boolean option as follows:

StringNames _StringNames = (StringNames)Enum.Parse(typeof(StringNames), _Name, true);

This will convert the string value "Bilal" contained in _Names, into a EnumType value, so now after executing the above statement, you have:


 _StringNames = StringNames.Bilal

So, StringNames.Bilal is an EnumValue equals the EnumValue _StringNames, so rewriting the above switch statement would be something as:

    string _Name = "Bilal";
    StringNames _StringNames = (StringNames)Enum.Parse(typeof(StringNames),_Name,true);

    switch (_StringNames)
    {
        case StringNames.Bilal:
            break;
        case StringNames.Wessam:
            break;
        case StringNames.Elsa:
            break;
        .......
    }

I want to thank my colleague Raed for uncovering for me the real return type of the Enum.Parse method.

Hope helps you out!

Regards

Tags: ,

May 5 2006

PopulateOnDemand &amp; Node Icon in TreeView - ASP.NET 2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 08:02

I discovered something interesting today in the world of TreeViews!

I have been working on TreeViews and dynamic populating for those TreeViews. One thing took my attention which is that:

When I first load the root node in a dynamically populated TreeView, having:

  • PopulateOnDemand true for the root node
  • ShowExpandCollapse true
  • ExpandDepth = "0"
  • Having XPFileExplorer ImageSet for instance (can be any other ImageSet)

The icon near the only node displaying now is a folder icon, in a time I should have a PC icon representing the root node.

But looking into Reflector, an advise given to me by my colleague Teemu Keiski, you will notice the following in the TreeNode - Render method:

      TreeNodeTypes types1 = this.GetTreeNodeType();
      string text5 = string.Empty;
      if (this.ImageUrl.Length > 0)
      {
            text5 = this._owner.ResolveClientUrl(this.ImageUrl);
      }
      else if (((num1 < this._owner.LevelStyles.Count) && (this._owner.LevelStyles[num1] != null)) && (style1.ImageUrl.Length > 0))
      {
            text5 = this._owner.GetLevelImageUrl(num1);
      }
      else
      {
            switch (types1)
            {
                  case TreeNodeTypes.Root:
                        text5 = this._owner.GetImageUrl(0);
                        goto Label_06FB;

                  case TreeNodeTypes.Parent:
                        text5 = this._owner.GetImageUrl(1);
                        goto Label_06FB;

                  case (TreeNodeTypes.Parent | TreeNodeTypes.Root):
                        goto Label_06FB;

                  case TreeNodeTypes.Leaf:
                        text5 = this._owner.GetImageUrl(2);
                        goto Label_06FB;
            }
      }

Notice the bold items:

GetTreeNodeType:
private TreeNodeTypes GetTreeNodeType()
{
      TreeNodeTypes types1 = TreeNodeTypes.Leaf;
      if ((this.Depth == 0) && (this.ChildNodes.Count > 0))
      {
            return TreeNodeTypes.Root;
      }
      if ((this.ChildNodes.Count <= 0) && !this.PopulateOnDemand)
      {
            return types1;
      }
      return TreeNodeTypes.Parent;
}

As you can see, the first if doesn't match, although the only root is at depth 0, but the children count now is 0.

The second if is false, we do have children count 0 but we have PopulateOnDemand true

Finally, the node type returned is Parent.

Going back to the Render method bold items, we can see that if the type is Parent we are rendering the Parent icon which is in our case (XPFileExplorer) a yellow folder.

This explains why the PC icon was not displayed initially. However, once you populate the children, it will be rendered again (the root node) as a PC icon.

If you feel bothered from having a folder icon at the beginng you can do the following in the Page_Load/Page_Init for instance:

this.TreeView1.Nodes[0].ImageUrl = this.Page.ClientScript.GetWebResourceUrl(typeof(TreeView), "TreeView_XP_Explorer_RootNode.gif");

Assuming you have placed a single node in the TreeView!

Hope this tip helps you all.

Regards

 

Tags:

May 5 2006

Localization in ASP.NET 2.0

Category: ASP.NET 2.0 - General | General | GeneralBil@l @ 06:12

I would like to invite you to check my latest article on the ASPAlliance.com.

The article is titled Localization in ASP.NET 2.0

Hope you will enjoy this article.

Regards

Tags: , ,

May 3 2006

Set Selection in TreeView - ASP.NET 2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 12:08

Assume the following: