Wednesday, March 21, 2007

Advanced User Control Creation Tips


Hi,

Today I am going to discuss about 2 approaches for UserControl creation in ASP.NET

My requirement was to create a control that can hold other controls as child control. In case of User Controls we don't have the option of using a container control like "Panel" as all the User Controls must inherit for the UserControl class.

I will explain a scenario, where it might make sense:
You are building a control which has lots of tables and images etc. So creating a custom control can be really difficult. But the actual requirement is to use this control as like a container control which can hold any other control. This is more of a requirement when you want to use the designer HTML view to create your page.

The approaches are
1. Create a templated control
2. Define a child control container for the User Control

both approaches are easy, but the templated one can be treated as the easiest. But this approach has a problem. The controls that are used inside the Template property cannot be referenced in the main class, that means you need to use the "FindControl" method to access these controls from code behind.

I will explain each approach in subsequent blogs

Labels:

Saturday, March 10, 2007

Hi,

I was trying to bring my blog in the eyes of search engines. I searched a lot for tips that will enable me to do that. Fortunately I found a site http://www.microsoft.com/smallbusiness/resources/marketing/online_marketing/help_people_find_your_web_site_8_tips.mspx

This site gives you enough tips for keeping your site known to search engines. Here are some URLs that you might be interested in:
Google: http://www.google.com/addurl/?continue=/addurl
Yahoo!: http://tinyurl.com/5oclp (registration required)
MSN: http://beta.search.msn.com/docs/submit.aspx

So... After a long gap of few months, I thought of writing a new blog. I am in Seattle, WA. The Rain City. It was too cold when I came here in January. Covered with snow for more than a week. I never went out of my home, since cold was too much for me.

I was working with one of my clients, who was into mobile applications. A new area, for me. It was even difficult than the web stuff. Here you need to write code that works in various devices that has different capabilities. When I was working with web, I thought browsers are the worst platform one can ever write applications for. Now I realised that there is always a worse thing than what you deal with.

The problems are so many. You need to think about WAP devices, XHTML enabled devices, small screen devices, black and white devices, and smart devices and what not. Frankly speaking this is a nut that is hard to crack. Each vendor has their own style of doing it.

So our client created a common platform that leverage the mobile .NET platform. It uses specific things like different CSS, different rendering etc for each specific device that falls out of the common-ally. Such a pain, you know, the testing...

So we use a new design pattern called Model View Presenter. This enables us to do proper unit testing without the hassles of ASP.NET page architecture. You can have different views with same presenter, which allows us to create Mock views for testing purpose. Checkout the benefits of using MVP model. Later I will write a full blog on how to go about leverging the maximum throughput of MVP.

Sunday, December 10, 2006

Hi,

I am Back. This time not with a coding issue, but with a unit testing trouble. I am a big fan of controlled unit testing. What I mean by this is, I don't waste lot of my time writing unit test cases. I first finds the critical paths in the code and writes the unit test cases for that. Somehow I feel that there should be a balance between the coding time and unit test case writing time. The rest of the flows I will test manually, which is more logical for me.

Now I have a problem. I am working on ASP.NET projects. Some of the program flow is in the base methods of ASP.NET Page class, e.g. : Page_Load, etc. These are difficult to test using the normal NUnit classes. I don't like the NUnitASP either. For me NUNitAsp, doesn't help in Unit Testing, but in function testing.

So, I came up with the idea of Generic Web Mocks that can help me in doing the "Unit Testing" of my internal methods.

It is a tedious process. So I am planning for another approach for doing a better unit testing of web internal methods.

I will discuss about this in another blog.

Wednesday, November 15, 2006

I actually hated coding for web. But, then I was forced to do that. This is because; we need to worry about a lot of problems unnecessarily. This includes browser capability, IIS configurations, permission problems etc. So I will be using this space to talk about these things:

Today I am going to talk about a problem that came across me:

In one of our project we had a problem where we need to change the Request Cookie in asp.net pages. This was a big task for us since there were around 100 pages and this has to be completed in less time. So modifying every page looked out of question.

Then I thought of using HttpModule to handle the situation by modifying the Request cookie.

After implementing this I realized a shocking factor about Request.Cookies. Whenever we modify the Respose.Cookies by removing an existing cookie, the Request.Cookies collection is reset to the original values. This means all the changes that we did to the response cookie is lost.

It was a nightmare for me to solve this issue. But fortunately I tried a total differnt way of solving this issue. I used Reflection to change the Response.Cookies collection object to a new CookieCollection and added all the Request.Cookies to it. All this happens in the BeginRequest event handler.

This way whenever the Response cookies are modified within the code anywhere else, the request cookies are not reset.

Here is the code snippet:

// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
// Your BeginRequest event handler.
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
ChangeRequest(context);
if(context.Request.Cookies["Context_Encr"]!= null )
{
HttpCookie cookie = HttpCookieEncryption.Decrypt(context.Request.Cookies["Context_Encr"]);
context.Request.Cookies.Remove("Context" );
cookie.Name = "Context";
context.Request.Cookies.Add(cookie);
}
}
catch
{
throw;
}
}

private void ChangeRequest(HttpContext context)
{
Type t = context.Response.GetType();
HttpCookieCollection col = new HttpCookieCollection();
for(int i =0;i< context.Request.Cookies.Count;i++)
{
HttpCookie c = HttpCookieEncryption.CloneCookie(context.Request.Cookies[i]);
col.Add(c);
}

//Just get the cookie collection and create a new Collection and assign to it
FieldInfo info = t.GetField("_cookies",BindingFlags.NonPublic BindingFlags.Instance);
if(info != null)
{
info.SetValue(context.Response,col);
}
}

// Your EndRequest event handler.
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
if(context.Response.Cookies["Context"]!= null )
{
HttpCookie cookie = HttpCookieEncryption.Encrypt(context.Response.Cookies["Context"]);
context.Response.Cookies.Remove("Context");
cookie.Name = "Context_Encr";
context.Response.Cookies.Set(cookie);
}
}
catch
{
throw;
}
finally
{
context.Response.Cookies.Remove("Context");
}
}

I think this helps somebody facing the same music ;)

Friday, October 20, 2006

A Session Data Loss problem (ASP.NET 1.1)

Today I am going to discuss about an issue that came up at one of our client site.

We developed an ASP.NET web application and deployed it at the client's place. We were using sessions to store data like User ID etc. If the session times out the session data will be lost and will result in automatic timeout for the user.

We came across a weird scenario,
Session timeout was set to 60 minutes. When the user come back to the site after 56 six minutes the session is extended. But if he leaves the browser like that for another 56 minutes, next time when he does any operation, the he was thrown out saying that the session is timed out.

We identified that this is not happening when the session is set to 20 minutes.

It took around 1 week for us to figure out what was going wrong.

The cause was that the data that was kept in the session was lost after 20 minutes of inactivity. This was because we were using "InProc" session state management.

This is apparently an ASP.NET issue (status is by design) for InProc session state type.
Ref: http://support.microsoft.com/default.aspx?scid=kb;en-us;324772

Actual cause was the for performance the application pool was configured to shutdown after 20 minutes of inactivity.
So in our case the worker process shuts down after 20 of inactivity. Hence all the data stored in Session will be lost.

Here is the solution that we used to get rid of the problem:

1. In the web.config change the session timeout settings (timeout="60") to 60 mins

2. In the IIS Management Console do the following:
AppPool configuration:
i. Select the AppPool that is configured for the web application
ii. Select Properties
iii. Go to the Performance tab
iv. Select the check box the "Shutdown worker processes after being idle for ( time in minutes):"
v. Change the value from 20 to 60 (It should be 60 or more)
Close the property window


I hope this solution would be helpful for somebody suffering from the same headache

Hi,

I am new to Blogging... I am a software engineer with around six years of experience. I want to use this space to share solutions that I have adopted in resolving technical issues.