Posts Tagged ‘Development Patterns’

ASP.NET User Control events tamed through Page_PreRenderComplete

October 9, 2009 Leave a comment

Something that has been bothering me for a really long time is now under control thanks to a blog post in the .NET Geek’s Blog entry on Understanding the Page Life Cycle in ASP.NET.  In a nutshell, here is the issue.  I prefer to create user controls that wrap standard .NET controls and then put role enabled and other code into the controls.  That code determines if the control makes it self visible or disabled along with some other nice to haves.

The problem is that when a page first draws and fires off its events, the user control events follow, and if I come into the page wanting a button to be disabled in certian instances, the user controls resolve their states after the page.  So if I have a button in a user control and expose its Enabled property through a get/set variable, I can never make it stick when the page first draws.

In the source code below, I am using the Page_PreRenderComplete event to test for page state and call an appropriate method.  In this case the PrepUIforAdd is called and it disables a number of buttons as well other things.  If I called PrepUIforAdd in the Page_PreRender event, the buttons would never stay disabled.  But calling it in the Page_PreRenderComplete event will result in the outcome I want when the page displays.

		/// <summary>
		/// PreRender is complete
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Page_PreRenderComplete( object sender, EventArgs e )
			if ( !Page.IsPostBack )

				switch ( this.CurrentPageState )
					case PageState.Add:
					case PageState.Edit:

				this.ManagePersonTabContainer.ActiveTabIndex = 0;

		/// <summary>
		/// Preps UI for adding a person
		/// </summary>
		private void PrepUIforAdd()
			this.txtPersonID.Text = (PersonIDConstant.Everybody.ToInt()).ToString();

			this.txtLastName.Text = string.Empty;
			this.txtFirstName.Text = string.Empty;
			this.txtLogin.Text = string.Empty;
			this.txtPassword.Text = string.Empty;
			this.txtInceptionDate.Text = string.Empty;
			this.txtLastModifiedDate.Text = string.Empty;
			this.txtLastModifiedByPersonName.Text = string.Empty;
			this.ddActive.YesNoID = 1;
			this.ddDeleted.YesNoID = 0;


			this.bDelete.Enabled = false;
			this.bNew.Enabled = false;
			this.bResetPassword.Enabled = false;
			this.bRoleLink.Enabled = false;
			this.bRoleUnlink.Enabled = false;

			List<EmailCollection> dt = GetCachedEmailList();
			if ( this.ErrorLevel == Status.Success )
				ddEmailType.DataSource = dt;
				ddEmailType.DataTextField = "EmailTypeName";
				ddEmailType.DataValueField = "EmailTypeID";

			this.txtEmail.Text = GetCachedEmailAddress( PersonIDConstant.Everybody.ToInt(), ddEmailType );


Highly Recommended WCF learning Resources

One of the most valuable resources from my point of view is the author and lecturer Michele Leroux Bustamante.  She has written a book published by O’Reilly called Learning WCF that is really a great step by step and practical approach to understanding the details and implications of WCF. 

She will start with a high level concept and drill down through to source code to illustrate her point.  And, because it is a learning experience, the code is in context with enough to help you implement it.  I am mentioning this because a lot of the books I have read on WCF are basically concepts mapped to islands of code, but nothing at a practical level to help me to tie them together.

One note about the book.  There is a new chapter out that covers Visual Studio 2008 and .NET 3.5, but unfortunately, there is a chance when you order online, you are not always garuanteed to get the second release.  I bought the first release last year and wanted the second release and ordered from Amazon and they still sent me the first release.  However, Michele says that really the only difference is chapter one and she did a deal with her publisher to provide that chapter in PDF form on her blog.  The link is:

Additionally she has done a 15 part lecture series on WCF published through Microsoft’s Webcast series.   These lectures are downloadable and really very well worth the time.  The link for the WCF Webcast series is:!780CD8FB86EF8F7C!14966.entry?wa=wsignin1.0&sa=16708577.

I am revising this post to include several other books I found really helpful along the way:

  1. Programming WCF by Juval Lowy – publisher O’Reilly
  2. Pro WCF by Chris Peiris and Dennis Mulder – publisher APress
  3. Essential Windows Communication Foundation for .NET 3.5 by Steve Maine – publisher Addison Wesley

Maine’s Essential Windows Communication Foundation was the hardest book for me to understand at the beginning because he does not take a cookbook but a conceptual approach.  However the more I learned, the more I began gravitating to his book.  I really like it, however I have gotten a lot out of all of the books.

Just as an FYI, in a related post, I am writing a series of articles on a practical WCF architecture. The first post is here.

Defect in Microsoft Sample Code Using Forms Authentication

April 14, 2009 Leave a comment

If you are trying to use Forms Authentication and want to pass additional user data, there is a defect in the sample code provided by Microsoft.

The two errors are, (1) – when creating the FormsAuthenticationTicket, you must include the path of the cookie and (2) – you must create the authentication cookie using Response.Cookies.Add.  the code provided below works:

protected void btnLogin_Click( object sender, EventArgs e )
bool isAuthenticated = IsAuthenticated( txtUserName.Text, txtPassword.Text );

if ( isAuthenticated )
string roles = GetRoles( txtUserName.Text );
FormsAuthenticationTicket CLAC = new FormsAuthenticationTicket( 1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes( 60 ), true, roles, FormsAuthentication.FormsCookiePath );

string encryptedTicket = FormsAuthentication.Encrypt( CLAC );

Response.Cookies.Add( new HttpCookie( FormsAuthentication.FormsCookieName, encryptedTicket ) );

Response.Redirect(FormsAuthentication.GetRedirectUrl( txtUserName.Text, false ));


I tried changing the code, and instead of calling Response.Redirect using the FormsAuthentication.FormsCookieName method using FormsAuthentication.RedirectFromLoginPage().  This did log the user on, but the user data never showed up.  The problem in using the Microsoft example code was that the the auth cookie never got written.  Using the Response.Cookies.Add() method to create the cookie, and including the path when creating the auth ticket is what solved that problem.

The blog post that helped me out was

Microsoft’s Entity Framework

I have been wrestling with programming patterns that bring data to the UI, and I am looking at three ways to do it:

  • the hand rolled datalayer
  • LINQ
  • Entity framework

In this series of posts, I’ll share lessons learned and bits and pieces of how to make things work, like the best way to use stored procedures in LINQ and so forth.

I also have a really great book to recommend.  It’s “Programming Entity Framework” by Julia Lerman.  This is a really great book.  She takes you through a very complex and feature rich subject.  The only nit I have to pick with this book is that many of the examples are wrong.  If you follow instructions, your stuff will break.  This is very unfortunate because at the earliest stages of the learning process, one never knows why something truly went wrong, or if you are learning something the wrong way.

I wish publishing companies would just print a few copies of the book and either give it to people or simply pay them to walk through all of the exersizes in the book and catch what is wrong.

Basically, it’s a nit and well worth the price of admission to this subject.

How we move data around is really important, and frameworks and design patterns help us.  The Entity Framework is a very important framework and design pattern to learn.  Microsoft has put a lot of money into it, and this framework has won the internal battle.  The LINQ team has been merged into the Entity Framework team and LINQ is basically going to tread water and quietly go away.

There is a lot to be said for the ability to look at a programming problem from a domain perspective.  I can’t tell you the number of times I have had to redirect high level design sessions away from the physical database into requirements and business rules.  One of the benefits of the Entity Framework is the way it influences design conversations. 

Dr. Peter Chen has written extensively about the structure of design elements feeding back into language and vice versa.  Here are links to a few of his papers:

  1. The entity-relationship model: Toward a unified view of data
  2. Entity-Relationship Modeling: Historical Events, Future Trends, and lessons Learned
  3. From Ancient Egyptian Language to Future Conceptual Modeling
  4. English Sentenence Structure and Entity-Relationship Diagrams
  5. The entity-relationship model – A basis for the enterprise view of data
  6. English, Chinese and ER Diagrams
  7. A Preliminary Framework for Entity-Relationship Models
%d bloggers like this: