Anything related to the .NET framework
05 Aug 2007

FindControl and Master Pages

Continuing with my earlier post on enums where I proved people wrong, I decided to prove another MVP wrong. Once again for those who are already in the know they can skip to example 2 below.

FindControl Basics

First off a primer on FindControl taken from the MSDN help: Searches the current naming container for a server control with the specified id parameter. Example: The following example defines a Button1_Click event handler. When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing page. If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. If TextBox2 is not found, "Control Not Found" is written to the page. private void Button1_Click(object sender, EventArgs MyEventArgs) { // Find control on page. Control myControl1 = FindControl("TextBox2"); if(myControl1!=null) { // Get control's parent. Control myControl2 = myControl1.Parent; Response.Write("Parent of the text box is : " + myControl2.ID); } else { Response.Write("Control not found"); } }

Searching Master Page content using FindControl

You could build a recursive find control method which searches master pages and content pages control internally looping through each control and checking the ID, but then you would need to also build one which takes logic for the offset overloaded version. Sounds like too much work, and I guess MS thought so too since it was never designed this way. Example 2: If you wanted to search the master page for a control you could do the following: this.Master.FindControl("ControlID") This will find any control in the master page which happens to have the ID "ControlID". This control can be a sub control of another control. Whats going on here? Well to understand, I downloaded the famous Reflector and searched Microsoft's Framework for this. protected virtual Control FindControl(string id, int pathOffset) { string str; this.EnsureChildControls(); if (!this.flags[0x80]) { Control namingContainer = this.NamingContainer; if (namingContainer != null) { return namingContainer.FindControl(id, pathOffset); } There is more to it and you can read it here (You'll need Reflector 5 or higher installed for that to work). The important thing to note is the recursion being done there!!! Thus we do not need to worry about it. There is a problem though, this searches the master page only. How do we get to the content page? Example 3: If you wanted to search for a control in the content page. Assuming our Content Place Holder ID is named "Content" you can then put that in FindControl followed by either $ or : and then the control you want to find. this.Master.FindControl("Content$ControlID") OR this.Master.FindControl("Content:ControlID") There you go, now you can find any control (nested or otherwise) on any content page :)
Tags: 
05 Aug 2007

Dynamically working with Enum's

Enums in .NET are very powerful in defining options. By default when you define an enum it automatically assigns them sequential integer values from 1 (if you don't specify a start value). So how do we work with these dynamically? Well some say you can't, and they are wrong. But first let me cover the basics, if you want to skip over this scroll down to example 6.

Basics Of Enum

Example 1

In this example First would be equal to 1, Second to 2 and Third to 3.
public enum Demo { First, Second, Third }
Example 2

In this example First is equal to 1, Second to 222 and Third by 986.

public enum Demo { First = 1, Second = 222, Third = 986 } 
Example 3

What’s nice is that if you just want to change the start position then you can define that only, so in this example First is 10, Second is 11 and Third is 12.

public enum Demo { First = 10, Second, Third } 
Example 4

Even better is the ability to decorate the enum with the "flag" attribute, set the numbers (Raymond Chen explained why this is not done automatically) and use it as bitflags. Note the integer values are in traditional flag values with None set to 0 and All set to the combined value.

[Flags] 
public enum Demo 
{ 
None = 0, 
First = 1, 
Second = 2, 
Third = 4, 
All = 7 
} 
Example 5

So how do we use those flags? The code below will output:

First, Third

The code is:

static void Main(string[] args) 
{ 
    Demo Enum = Demo.First | Demo.Third; 
    Console.WriteLine(Enum); 
} 


Dynamically Using Flag with Enums

So here we are basics out of the way, and now on to the fun. I continue to use the definition in example 4 above.

Example 6

First I will show how to add a value to the enum variable. What I do is start off by defining the enum to none (0 value) then using the OR concat (|=) symbol I add each enum. This code will output:

Second, Third

The code is:

static void Main(string[] args) 
{ 
    Demo Enum = Demo.None; 
    Enum |= Demo.Second; 
    Enum |= Demo.Third; 
    Console.WriteLine(Enum); 
} 
Example 7

In this last example I will show how to remove an value from the enum variable. I start off by defining all (integer value of 7) and then I use the AND concat (&=) symbol and prefix the enum value with tilde (~). This code will output:

First, Third

The code is

static void Main(string[] args) 
{ 
    Demo Enum = Demo.All; 
    Enum &= ~Demo.Second; 
    Console.WriteLine(Enum); 
} 
15 Jun 2007

Microsoft is pushing open source

Before I begin let me clarify a few points so you are aware of the context of this post.
1) I currently work for a Microsoft Gold Partner, been contracted to Microsoft (through my employer) and spoken at TechEd.
2) Before that I worked for company which used almost solely open source software and developed software for that. I also did work with the Shuttleworth foundation at the linux days event.
So I have been on both sides of the fence, now on to the content...

Go anywhere near Slashdot and mention Microsoft and you will get atleast a few zealots who complain about it's attitude/actions to FOSS (Free and Open Source Software).

Well I am not going into the free part, since until I can live without money making everything free doesn't help and there is many people in the traditional OSS community which do make money (RedHat, Novell, many linux contributors etc...) so I guess I am not alone in this view.

But many people bring up the open source side, which makes less sense to me. Microsoft does have this shared source thingy which is some special license for special people, but that doesn't fit with true OSS where anyone can access it so I'm not including it. Microsoft has Port25 (there public front for their internal open source lab) which has some interesting information, but not really pushing OSS. There are also a few "sponsored" projects on SourceForge and the now defunct CodePlex. Neither of these push OSS as part of major projects. So while benefical aren't big enough.

So what is it that doesn't make sense to me? Simple, the .Net framework is completely open source. All of the .Net assemblies are in source code (IL) all the time and thanks to reflection can be transformed into a convient language of choice of the viewer. Since .Net is the big push from Microsoft the new core of their systems will be open source. This can be seen currently with their applications built on top of it, like Microsoft CRM which has all their assemblies in .Net and can all be opened and viewed. Microsoft CCF is even further advanced with it including some applications in .Net (like the admin console) but the bulk is available in direct source code. BTW Neither are obfuscated in anyway, so there is no attempt to close this source.

Agreed that the core big money makers (Windows, Office etc...) are still closed source, but how much of that is legacy versus how much is based on the choice of language tool (C++ Win32 vs Dot Net) and how much of that is specific plans to close off the source I can not say and no one outside the core executive at Microsoft could say, but the argument that Microsoft doesn't get it, is just wrong. They get it and probebly more than most of the zealots mentioned earlier since they have figured out to use it strategically.

09 Feb 2007

Set classes in BCL

Mark Seemann has started a vote for the introduction of sets into the BCL. Funny enough I have not thought about them since the Delphi days and got on using generics in .Net and some extra items tacked on the top, but now that Mark has brought it up I can think of dozens of places in code this would have been useful. For more details on this view Mark's post or if somehow I have convinced you you can go and vote directly.

Tags: 

Pages