Archive for May 7th, 2006

07 May 2006

Murphy’s Laws of .NET

9 Comments .NET, ASP.NET, Fun & Humorous

I have written Murphy’s Laws of .NET based on my previous experience with .NET (using VB.NET & C#):

1. When coding .NET application, whatever happens, behave as though you meant it to happen.

2. When you get to the point where you really understand those new features of .NET, it’s probably obsolete.

3. For every feature in .NET, there is an equal and opposite malfunction.

4. There will be six ways to do same thing in .NET, and you always discover that you have choose the worst one, but only at the final stage of project.

5. A .NET program will always do what you tell it to do, but rarely what you want to do.

6. He who laughs last probably uses simplest architecture of developing .NET application.

7. A complex .NET application that does not work is invariably found to have evolved from a simpler VB 6.0 system that worked just fine.

8. The number one cause of any .NET based problem is another .NET based solution.

Do you identify yourself somewhere in these laws?

07 May 2006

The definition of a consultant

1 Comment Fun & Humorous

The definition of a consultant:

Someone who borrows, your watch, tells you the time and then charge you for this privilege.

07 May 2006

Passing values between pages

4 Comments ASP.NET

ASP.NET 1.1
There are three widely used methods of passing values from one page to another in ASP.NET 1.1.

1. Using Query String
We usually pass value through query string of the page and then this value is pulled from Request object in another page.

FirstForm.aspx.cs
Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);

SecondForm.aspx.cs
TextBox1.Text = Request. QueryString["Parameter"].ToString();

Very straightforward! Only problem with this method is that we need to pass value under the constraint of maximum characters (read 2083 characters) allowed in URL. If you pass string larger than that, your application will going to crash. So it is reliable only when you are passing integer kind of value or if you are sure about size of value will not exceed maximum URL length. If parameter is larger than maximum URL length or if you are not sure about its length, then prefer not to use query string to pass value.

Another problem is that user can actually see the passing parameters and its value in address bar which further require to validate each parameter after pulling it from Request object.

You also have to take care of any special characters in the value while passing it through query string. So you must encode the value before passing it to next page. And likewise, you decide that passed value after pulling from Request object. So our code snippet of pulling passed value will be something like this:

FirstForm.aspx.cs
Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));

SecondForm.aspx.cs
TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());

This approach is very handy if you have to pass some integer like values, for example like employee id, order id etc.

2. Using Context Object
Passing value through context object is another widely used method.
FirstForm.aspx.cs
TextBox1.Text = this.Context.Items["Parameter"].ToString();

SecondForm.aspx.cs
this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);

Note that we are navigating to another page using Server.Transfer instead of Response.Redirect.

One of the major benefits of using Context object for passing values is that passed value will not shown to user at address bar while navigating to next page. This means developer needs not to worry about validating parameter value after pulling from Context object. There is no limitation of size of parameter passed using Context object as it method does not depend upon URL of page.

Some developers also use Session object to pass values which I never prefer at all. In that method, value is store in Session object and then later pulled out from Session object in Second page.

3. Posting form to another page instead of PostBack
Third method of passing value by posting page to another form, like what we were doing in ASP age. Here is the example of that:

FirstForm.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
buttonPassValue.Attributes.Add("onclick", "return PostPage();");
}

And we create a javascript function to post the form.

FirstForm.aspx
<script language=javascript>
function PostPage()
{
document.Form1.action = "SecondForm.aspx";
document.Form1.method = "POST";
document.Form1.submit();
}
</script>

SecondForm.aspx.cs
TextBox1.Text = Request.Form["TextBox1"].ToString();

Here we are posting the form to another page instead of itself. And pulling value of control of previous page through Request object.
NOTE: You might get error “The viewstate is invalid for this page and might be corrupted.” in second page using this method. Workaround to handle this error is to put EnableViewStateMac=false in Page declaration of form

SecondForm.aspx
<%@ Page language="c#" Codebehind="SecondForm.aspx.cs" AutoEventWireup="false" Inherits="WebApplication17.SecondForm" EnableViewStateMac=false%>

ASP.NET 2.0
In ASP.NET 2.0, Microsoft has solved this problem by adding PostBackURL property of control for cross page post back. Implementation is a matter of setting one property of control and you are done.

FirstForm.aspx
<asp:Button id="buttonPassValue" style="Z-INDEX: 102;" runat="server" Text="Button" PostBackUrl="~/SecondForm.aspx"></asp:Button>

SecondForm.aspx.cs
TextBox1.Text = Request.Form["TextBox1"].ToString();

In above example, we are assigning PostBackUrl property of the button we can determine the page to which it will post instead of itself. In next page, we can access all controls of the previous page using Request object.

You can also use PreviousPage class to access controls of previous page instead of using classic Request object.

SecondForm.aspx.cs
TextBox textBoxTemp = (TextBox) PreviousPage.FindControl("TextBox1");
TextBox1.Text = textBoxTemp.Text;

As you have noticed, this is simple and clean implementation of passing value between pages.

Passing values between pages is another common task accomplishes in web based development. As we have discussed many of mechanisms above, I always prefer and recommend to use posting form to another page then other methods, if its web development in ASP.NET 1.1. If it’s in ASP.NET 2.0, then I prefer to use PostBackURL for its clean implementation.

07 May 2006

Definitive list of design pattern links

No Comments Uncategorized

Here is a huge list of links pertaining to design pattern complied by Carl Ã…sman. You will definitely find this collection very useful.
URL: http://www.edlin.org/cs/patterns.html

07 May 2006

How to preventing multiple clicks?

3 Comments ASP.NET

Some of the operations in web based application requires bit more time to process. Operations like submitting an order, or entering & validating any credit information, processing for a telecom service etc. usually involves more backend process which sometime results multiple clicks by user and hence you often find multiple records for same request. Most widely used solutions to preventing multiple clicks by user is disabling submit button using javascript on first click, so that user cannot click next time.

Some developers handle this issue through redirecting user to another page to display some kind of processing status to prevent multiple clicks. This works fines in most cases but still majority of developers wants their user to stay on same page while application is processing their request.

Problem with disabling any server side button in ASP.NET based web application using javascript code is that you will then not able raise any server side event of that button. And hence, ASP.NET will not raise onclick event of the button where you have place your process code.

But here is a good hack of disabling any server side button and still you will able to raise its server side events.

To start with, first drop a button in page with name say “buttonOrder” and label “labelStatus”. You want that user should not able to click on this button twice. And for that you first want to disable this button at client side before processing requested operation at server side. Simply add an attribute to this button for onclick client side event in the Page_Load event of the page. Here is the code:

private void Page_Load(object sender, System.EventArgs e)
{
buttonOrder.Attributes.Add("onclick", "buttonOrder.disabled=true;" + this.Page.GetPostBackEventReference(buttonOrder).ToString());
}

private void buttonOrder_Click(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(10000);
labelStatus.Text = "Processed.";
}

Read more