Tuesday, June 17, 2008

Access master page members from a page

download the source code

Sometimes we need define a property or a method in a masterpage and we need access to that property or method in pages that are using that masterpage. To do this we have two choices.

1- Using FindControl (this.Master.FindControl("usercontrolid"))

2- Using this.Master.YourProperty

The first one is not type safe so you could misspelled a property name then you will get null so this approach is error porn. Furthermore, the control should be cast to a valid control. Also you cannot access property by this approach but you can access to controls in masterpage. However, it does not need any other effort.

The second one is type safe, access to masterpage properties is possible and there is no need for casting. However, it needs that the control be defined at least internal and you need define the master type in your page. Then compiler can detect that master page property and it allows you to access to those property through intelisence.

Let's see an example: we have a Label and image in our masterpage and we have some methods in our master page. We need to call these methods from page that is using these masterpage. Also we have a property in master page that we need to have access in our page. Take a look at this:

This is the master page html code:

This master page properties and methods

public Label TitleLable

{

get

{

return LabelTitle;

}

}

public void ShowImage()

{

Image1.Visible = true;

}

public void HideImage()

{

Image1.Visible = false;

}

Then we create a page that is using this masterpage. And this is the html code:

Important: in this code we defined a mastertype directive to notify compiler that we are using these masterpages then if you compile and try to access masterpage methods and properties you can see them.

Take a look to the code behind of the page. As you see we implemented both way. I highly recommend the second way. Remember the first way does not force you to define mastertype in the page. While, second way need the mastertype directive in html code of the page:

protected void ButtonChangeTitle_Click(object sender, EventArgs e)

{

//first way

//Label lbl = Master.FindControl("LabelTitle") as Label;

//lbl.Text = "New Title is Assigned";

//second way

Master.TitleLable.Text = "New Title is Assigned";

}

protected void ButtonHideImage_Click(object sender, EventArgs e)

{

//first way

//Image img = Master.FindControl("Image1") as Image;

//img.Visible = false;

//second way

Master.HideImage();

}

protected void ButtonShowImage_Click(object sender, EventArgs e)

{

//first way

//Image img = Master.FindControl("Image1") as Image;

//img.Visible = true;

//second way

Master.ShowImage();

}

As I said before I highly recommend the second way. Let's review the steps:

1- Create a master page

2- Define some public properties and methods in masterpage

3- Create a page using the master page

4- Define the mastertype directive in page

5- You can access masterpage public properties and method through Master object in the page

Operators in C#

It is important to know that C# and java support the rules of IEEE, means if you have 10.5/0 you won't get an error. It will be infinity or if you have 0.0/0.0 the result will be not a number. But if you have 10/0 then you will got an overflow error.

As you all know there are different kinds of operators. 
 ·         Relational Operators such as ==,!=,>,>=,<,<= which are used between two operands(except !=) and the result is a Boolean.
 ·         Logical operators such as &,,&&, that are used between two Boolean operands and return a boolean. It is good to point that && and  make a short circuit, means if the first operand is false the && operator returns false without checking the other operand. But if the first operand is true  operator will return true without checking the other one.
 ·         Arithmetic operators such as +,-,*,%. No need to describe them more as you are familiar with 
 
Besides the other operators that we don't describe them here, there are some important ones that are basic in C#.

++ Increment operator, incrementing the value by 1 (i++ is the same as i=i+1)

— Decrement operator, decrementing the value by 1 (i— is the same as i=i-1)

+= Increment assignment operator (i+=5 is the same as i=i+5)

-= Decrement assignment operator (i-=5 is the same as i=i-5)

== and != Equality and Inequality operator

% Modulus (remainder) operator. (17%5 equals 2)

Thanks to Azadeh for this post