September 25, 2009

In the previous example -Pass Data from MasterPage to content page -, I showed how to pass data from master page to content page by creating properties in the master page. There, I also mentioned that data can be passed from master to content page using Master.FindControl() method. In this example, I will use this method pass data.

I will use the same master and content page as in the previous example - the only place that needs to change is the Button click event that finds and sets the value of the Label control in the master page.

Here, is the code for the page.

In this event, I am creating a Label control and assigning it to the master page's Label control. Then I am setting the Text property of this Label control to the text entered in the TextBox control.

Data from master page can be passed to content pages either by using Master.FindControl() method on the page or by creating public properties in the master pages. The properties created need to be public, otherwise the page will not be able to access them. In this code snippet, I will create properties in the master page and access them from the content pages. I am also using the same master and content pages as in Create and Use Master Page and have the pages modified to suit this example.

Here, is the code for the masterpage.

The master page, "Content.master", contains a Label control which will be accessed from the content page. The property, "Label1_Data", related to the Label is in the code behind page below.

To use the property "Label1_Data" from the content page, the content page must use "Content.master" as the master page and also declare the MasterType directive and set the virtual path to the path of the master page. Here, is the code of the content page.

The content page contains a Label control to display the data from "Label1_Data" property, a TextBox control that allows "Label1_Data" to be updated, and a Button control to perform postback. Here is the code behind page.

September 23, 2009

In this example, I will show how to use AJAX UpdateProgress control to display a message when a page is redirected to a different page. We can display a message either using javascript or by using meta refresh. With javascript the drawback is that a popup may need to be displayed or the user may have disabled javascript. With meta tag, the refresh will fire after the page is loaded. That is, it is independent of other events. For example, we cannot use meta refresh if we are redirecting the page on Button click.

In this page, we will have a Button, ScriptManager, UpdatePanel and UpdateProgress controls. Remember to add ScriptManager control, since otherwise the page will not work. The Button control will be ContentTemplate of the UpdatePanel and the UpdateProgress will be called when the Button is clicked.

Here, is the code for the .aspx page.

As you can see, when the Button is clicked, Button1_Click event is called. In the method, I am using Response.Write("..."). However, this did not write anything on the browser. This only writes after the method finishes - so we cannot use this.
Then, I am pausing the function for 5 seconds by calling System.Threading.Thread.Sleep(5000);. This is necessary to show the message in the UpdateProgress. If the event is not paused, the page will redirect without displaying any message.
Finally, I will redirect the page by calling Response.Redirect("url");.

Here is the code for the page.


When the page is run and the button clicked, the page is paused for 5 seconds andthe redirect message is shown by the AJAX UpdateProgress control.

September 22, 2009

Create and Use Master Page

Master page was introduced in Asp.Net 2.0 and defines the layout and markup that is common to all content pages. A
content page is one that is bound to a master page. Creating markup of all the pages in one master pages makes it easy
for designers to modify changes to the layout of a website easily by modifying the master page.

A master page has a .master extension and can contain text, html and server controls. It contains a @Master directive instead of a @Page directive. The default master page (default template created by Visual Studio) includes the html, head and form tags. It also includes one or more ContentPlaceHolder controls to enable content pages to insert content. The master page inherits from the MasterPage class.

To add a master page to a website in Visual Studio, add a new item, and choose the MasterPage. The prompt will look like below.

The default page will look like below.

I will update the master page to have default header and footer. The master page will look below.

Once the master page has been saved, the content pages can be created. Add a new .aspx page by adding new file.

Then choose the master page for the page and click ok.

The resulting page will look below.

Notice that there are 2 content controls, one for head section and one for body section as defined in the master page. To add content to the page, content will need to be added to the content control. Here, I will add a Label control and
some html. The resulting page will look like below.

When the page is run, the page looks like this.

As can be seen, the header and the footer comes from the master page while the second line is generated by the content page.

September 1, 2009

In this example, I will show how to allow certain file types to be uploaded via the FileUpload control. For example, we may need a FileUplaod control that only allows word documents and text (.txt, .doc) files to be uploaded and rejects files with other file extensions.

In .aspx page, drag a FileUpload control, Label control and a Button control. The FileUpload control allows user to be uploaded, the Label control will display message whether the file can be uploaded or not and the Button control's OnClick event will allow the file to be posted back.

Here is the code for the main page,

Here is the code for the SaveFile event.

When the SaveFile event is run, another method GetExtension(string filename) is called that checks for the extension and displays a status. To save the file, uncomment the line FileUpload1.SaveAs...

In GetExtension(string filename) method, I declared a string array containing valid file formats. Then I create System.IO.FileInfo object using the string filename (of the uploaded file). Then, I check for the extension of the file.

Reference: Shahed Kazi at