Find Controls in an ASP.NET Page with a MasterPage

5. September 2011 01:07 by Mahmoud Farhat in ASP.NET, C#  //  Tags:   //   Comments (0)

If you are seeking to access control in a master page or aspx page files you have to use FindControl method.

The following code examples shows how to use the FindControl method to get a reference to controls so when using MasterPage file you will find controls rendered with prefix such as “ctl00$<ContentPlaceHolderID>$<NameYouGaveToControl>” according to their place in the page hierarchy so you must get reference to the ContentPlaceHolder first and then use FindControl mehtod to locate the control.

Example:

ContentPlaceHolder mpContentPlaceHolder = 
	(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (mpContentPlaceHolder != null)
     Button mpbtnSave = (Button)mpContentPlaceHolder.FindControl("btnSave");

 Gets a reference to a Button control that not in a "ContentPlaceHolder".

Example:

Button mpbtnSave = (Button) Master.FindControl("btnSave");

Gets a reference to a Button control that is in aspx page and not in a MasterPage.

Example:

Button mpbtnSave = (Button) Page.FindControl("btnSave");

Listbox - Change Item's Color

4. September 2011 15:32 by Mahmoud Farhat in ASP.NET, C#  //  Tags:   //   Comments (0)

If you have two collection lists one of them for parent (Main Category) and other one for child (Sub Category) and you want to display both of them in one listbox but you want the parent item take back color different than the child, the below example code is showing how to do this.  
 
//loop for parent collection list
foreach (var parentItem in objParentCollectionList)
{
   //Set all back color items with blue
   listBox1.BackColor = Color.Blue;
   //inset parent items in listbox control
   listBox1.Items.Add(new ListItem(mainItem.Name, mainItem.ID.ToString()));
 
   //Get all childs by parent ID
   objChildCollectionList = GetChildList(mainItem.ID);
   if (objChildCollectionList != null)
   {
       //loop for child collection list
       foreach (var childItem in objChildCollectionList)
       {
           ListItem listItem = new ListItem(childItem.Name, childItem.ID.ToString());
           //change back color foe each child item
           listItem.Attributes.Add("style""background-color: Azure");
           //inset child items in listbox control
           listBox1.Items.Add(listItem);
        }
    }
}
 
Note:
listbox1.BackColor = Color.Blue; it will effect on all listbox items not on specific item

Difference between int.Parse() and Convert.ToInt32

9. April 2011 10:36 by Mahmoud Farhat in C#, C#, C#, C#  //  Tags:   //   Comments (0)

 

This post just a slight tip for showing the difference between Int32.parse(string) and Convert.ToInt32(string)

 

Int32.parse(string)

- Int32.Parse(string str) method converts the string representation of a number to integer 32 bit.
- when str is a null reference it will throw ArgumentNullException.
- if str is other than integer value it will throw FormatException.
- when str represents a number less than MinValue or greater than MaxValue it will throw OverflowException.

Convert.ToInt32(string)

- Convert.ToInt32(string str) method converts the string representation of a number to integer 32 bit.
- when str is a null reference it will return 0 rather than throw ArgumentNullException.
- if str is other than integer value it will throw FormatException.
- when str represents a number less than MinValue or greater than MaxValue it will throw OverflowException.

C# 4.0 optional parameter and named arguments

17. January 2011 00:25 by Mahmoud Farhat in C#, C#, C#, C#, C#, C#, C#, C#  //  Tags:   //   Comments (0)

C# now supporting optional parameter in methods, constructors, indexers and this is new feature coming in C# 4.0 with additional feature named arguments

- Optional parameter:

The optional parameters idea is that the arguments on a method may have default values and therefore it is unnecessary to pass those arguments in a method if you are willing to accept those default values.  And this may be helping you when you are going to overload methods several times.

Example:

public static void UserInfo(string name, int age, string country = "Egypt”) {
            Console.Write(" Name:{0} \n Age:{1} \n Country:{2}",name,age,country);
        }

In the method above we set a default value in country, which means it can now be used optionally by caller of the method

UserInfo(“Mahmoud”,30); 

The country is not specified and this means you are accept to take the default value and then you can handle you work flow as you.

Output:
Name:Mahmoud
Age:30
Country:Egypt

or you can override the country by passing country name  

UserInfo(“Mahmoud”,30,”USA”); 

Output:
Name:Mahmoud
Age:30
Country:USA

- Named parameters

Sometime you have multiple optional parameters

public static void UserInfo(string name, int age, string country = "Egypt", _

       string city = "Cairo"){}

And the question here how do you specify the value of city without specify the value of county??!!

We can do that by “Named Parameter”, and this means that The parameter for each argument can be specified by parameter name

UserInfo("Mahmoud",30,city:"Alex");


Note:

The optional parameters must come at the end of the list of method arguments. And you have to specify all your required arguments in the method first and then list the optional arguments last just like above samples.