Archive for May 28th, 2006

28 May 2006

Working with dynamic controls – Basics

4 Comments ASP.NET

Working with dynamic controls in ASP.NET is real pain for developer. First you have to create instance of control, then to configure all properties of control through code and then to look for any container control where you can drop this dynamic control. Interestingly, you also have to recreate same dynamic control on every PostBack.

Let’s start will simple example to work with dynamic control:

Label dynamicLabel = new Label();
dynamicLabel.ID = "dynamicLabel";
dynamicLabel.Width = Unit.Pixel(200);
dynamicLabel.Text = "Test Text";
Page.Controls.Add(dynamicLabel);

This code will simply add a dynamic label in Page.

But how we can position this control to specific location in the page? That should be easy, we can use Top & Left property of this control! But if you check the properties of Label, there is no Top or Left property available. So then how we can set its location?? Well, using style attribute of control. But for that you also have to set control position style as absolute. Like this:

Label dynamicLabel = new Label();
dynamicLabel.ID = "dynamicLabel";
dynamicLabel.Width = Unit.Pixel(200);
dynamicLabel.Style.Add("POSITION", "absolute");
dynamicLabel.Style.Add("TOP", "100px");
dynamicLabel.Style.Add("LEFT", "200px");
dynamicLabel.Text = "Test Text";
Page.Controls.Add(dynamicLabel);

Read more

28 May 2006

Automatically redirecting user to login page on session timeout

2 Comments ASP.NET

As most of the locking mechanism works in window based application, like if there is no activity by user for say 10 minutes, application automatically pops up a login dialog box to re-authenticate himself again. Whereas in web based application, being a stateless environment, its bit difficult to implement same kind of feature of auto redirecting user to login page after specific time duration.

But if we can utilize refresh attribute of meta tag, we do can achieve same kind of page flow in ASP.NET. Just put below meta snippet in header of your page.

<meta http-equiv="refresh" content=" 600;url=http://www.application.com/Login.aspx?Redirect=<%= Request.Url.AbsoluteUri %>">

First value of content attribute is number of seconds (10 minutes in above case) page will wait before redirection and next value specified the URL where page will redirect (here it will redirect to the Login.aspx page).

So test this application, minimize the wait to lesser time say 20 seconds, and run the application. Page will automatically redirect to specified page.

28 May 2006

Guideline on Object Design

1 Comment C#, Programming

Alan has written a series of some good class design and implementation guidelines post.
URL: Object Design and Implementation

Rule #1: Practice Drawing Make Good Classes
Rule #2: Documation!
Rule #3: Alan’s Class Template (“ACT”)
Rule #4: Never Walk, But Fly!!!
Rule #5: Always evaluate How We Fly!
Rule #6: No Private Variable Shall Have Direct Access to the Class Public Interface
Rule #7 & #8: All about Numbers!!