<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C#: Best Practices</title><link>http://blogs.clearscreen.com/enadan/category/162.aspx</link><description>Of course I think they are, but feel free criticizing them. </description><managingEditor>Daniel de Andres</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Daniel de Andres</dc:creator><title>ASP.NET Best Practices</title><link>http://blogs.clearscreen.com/enadan/archive/2006/04/11/2978.aspx</link><pubDate>Tue, 11 Apr 2006 11:41:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/04/11/2978.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2978.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/04/11/2978.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2978.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2978.aspx</trackback:ping><description>&lt;p&gt;With this little article I don't want to enumerate the common Best Practices in C# (there are so many articles and blog entries about it, and most of them are really good). I don't want to discuss about using StringBuilder instead of concatenating strings, Arrays vs ArrayLists and so on, I want to share with you some tips I'd learn in my short but intensive experience developing ASP.NET applications.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;All C# Best Practices are valid for ASP.NET, use them. &lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;I don't know why but I'd seen in several projects all pieces are really good except the user interface layer. The data access layer is always the best, with all OOP standards and best practices applied there, also happens with the business components, the horizontal components, the architecture.... What happens with user interface? Why we code ASP.NET pages in a different way? I know this is the most difficult layer for programmers, because you have to make a windows application in a web environment (bad design), using JavaScript to avoid traffic, showing and hiding controls depending on user roles, but this is not an excuse. I'd seen several times string concatenating in ASP.NET pages to build a script function, but never (or almost never) had seen it to build a dynamic query in the data access layer. Why? &lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pages are also Objects.&lt;/strong&gt; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;Everything in .NET is an Object, but most of the code-behind classes I'd ever seen are poor designed. Thinking on ASP.NET pages like objects reduce requirements changes development effort, bug fixing and refactoring. Think you have a class that always invoke some methods from another class in the same order (this is the ASP.NET life cycle and the invoking class is the CLR) and, depending on user actions, one or more methods can be invoked in the middle of this sequence. You know exactly when each method will be invoked, so developing ASP.NET pages shouldn't be so difficult. &lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Properties, you can use them.&lt;/strong&gt; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;I know all of you had used properties in almost 100% of the classes you had developed. In ASP.NET pages I'd never seen a property, just fields. If you use properties to encapsulate and centralize the use of this fields (usually private fields) when developing ASP.NET pages you can improve your design and code will be more readable and maintainable. OK, you have a problem with properties in ASP.NET, you can't store the value of the property in a private field storaged in memory.... really? You can use ViewState, Session, Cache, Application.... instead of a private field to guess it. You can see an example in a previous post: &lt;a class="postTitle2" id="viewpost.ascx_TitleUrl" href="/enadan/archive/2006/02/02/2747.aspx"&gt;&lt;font color="#56b6e9"&gt;Web Forms are also Objects...&lt;/font&gt;&lt;/a&gt; &lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Handling Events (No Business, just events).&lt;/strong&gt; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;When I say events I want you to think on control events (DropDownList.SelectedIndexChanged, Button.Click...). Events are the only way the user can interact with the pages, but this is design, no Business. I think is a best practice extracting the logic usually coded in the event handlers into private methods. You have a private method that adds a new row in a grid, but you don't care if this line should be added when the user clicks on a Button or when a DropDownList changes it selection. From this event handlers, you should invoke the private method to add the new row, but I think is better not to code directly in the event handler. Doing this way, changes in user interface will be easyer to develop.&lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IsPostBack… USE IT!!!&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;Every line of code outside an "if (!IsPostBack)" condition should be justified. Of course, if you're handling control events you're in a PostBack. I mean each line of code placed in the common life cycle methods (Init, Load, PreRender, Render...). This methods will be invoked in every PostBack, so you must be really sure that this line is really needed out of a not IsPostBack condition, or you will be executing more than required, loosing performance and increasing bugs (more lines executed, more bugs).&lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Don’t code for UI, code for Business.&lt;/strong&gt; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;If you think on Databases design, this is obvious. Lets apply it to the other interface, the UI. I think there are two interfaces in distributed applications, the Database and the User Interface. Just before store data, and just before show it to the user, we usually need to format it to the appropriate data type (or format). But we should avoid to use a string to store a date value just because Database need it or the user prefers to see it in a different format. I mean, use always strongly typed entities and properties all along your application, and make the needed changes as late as possible. Doing this way, you don't have to worry about the correct format of the string containing a date value. In the other hand, think the same way parsing input data from the user, but doing it as soon as possible.&lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do the simple, do the best.&lt;/strong&gt; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;Notepad never fails. Word never...., sorry, I can't lie. I can put lot of examples that demonstrate this tip, but I think this is the easy one. The really difficult thing is not to forget it.&lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Read, learn and think.&lt;/strong&gt; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;IT world is moving quickly and get changes each day. You don't have to be an expert on each new technology (I think is impossible), but you need to know something about where IT trends. Just knowing several ways you can choose the correct one. I think is more important to know what it could be done than the way it could. If you know something can be done, you can ask your colleagues, Google, forums, blogs... I'm sure you will finally find a solution. If you don't know something can be done.... you can't find the way.&lt;/p&gt;
&lt;p&gt;As you can see, this is not a standard Best Practices article. Feel free commenting each tip, I hope you do it to get a better final version of this tips adding to them all your knowledge.&lt;/p&gt;
&lt;p&gt;Thanks for the time.&lt;/p&gt;&lt;/blockquote&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2978.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>With this little article I don't want to enumerate the common Best Practices in C# (there are so many articles and blog entries about it, and most of them are really good). I don't want to discuss about using StringBuilder instead of concatenating strings, Arrays vs ArrayLists and so on, I want to share with you some tips I'd learn in my short but intensive experience developing ASP.NET applications.</p>
<ul>
<li><strong>All C# Best Practices are valid for ASP.NET, use them. </strong></li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>I don't know why but I'd seen in several projects all pieces are really good except the user interface layer. The data access layer is always the best, with all OOP standards and best practices applied there, also happens with the business components, the horizontal components, the architecture.... What happens with user interface? Why we code ASP.NET pages in a different way? I know this is the most difficult layer for programmers, because you have to make a windows application in a web environment (bad design), using JavaScript to avoid traffic, showing and hiding controls depending on user roles, but this is not an excuse. I'd seen several times string concatenating in ASP.NET pages to build a script function, but never (or almost never) had seen it to build a dynamic query in the data access layer. Why? </p></blockquote>
<ul>
<li><strong>Pages are also Objects.</strong> </li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>Everything in .NET is an Object, but most of the code-behind classes I'd ever seen are poor designed. Thinking on ASP.NET pages like objects reduce requirements changes development effort, bug fixing and refactoring. Think you have a class that always invoke some methods from another class in the same order (this is the ASP.NET life cycle and the invoking class is the CLR) and, depending on user actions, one or more methods can be invoked in the middle of this sequence. You know exactly when each method will be invoked, so developing ASP.NET pages shouldn't be so difficult. </p></blockquote>
<ul>
<li><strong>Properties, you can use them.</strong> </li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>I know all of you had used properties in almost 100% of the classes you had developed. In ASP.NET pages I'd never seen a property, just fields. If you use properties to encapsulate and centralize the use of this fields (usually private fields) when developing ASP.NET pages you can improve your design and code will be more readable and maintainable. OK, you have a problem with properties in ASP.NET, you can't store the value of the property in a private field storaged in memory.... really? You can use ViewState, Session, Cache, Application.... instead of a private field to guess it. You can see an example in a previous post: <a class="postTitle2" id="viewpost.ascx_TitleUrl" href="/enadan/archive/2006/02/02/2747.aspx"><font color="#56b6e9">Web Forms are also Objects...</font></a> </p></blockquote>
<ul>
<li><strong>Handling Events (No Business, just events).</strong> </li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>When I say events I want you to think on control events (DropDownList.SelectedIndexChanged, Button.Click...). Events are the only way the user can interact with the pages, but this is design, no Business. I think is a best practice extracting the logic usually coded in the event handlers into private methods. You have a private method that adds a new row in a grid, but you don't care if this line should be added when the user clicks on a Button or when a DropDownList changes it selection. From this event handlers, you should invoke the private method to add the new row, but I think is better not to code directly in the event handler. Doing this way, changes in user interface will be easyer to develop.</p></blockquote>
<ul>
<li><strong>IsPostBack… USE IT!!!</strong></li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>Every line of code outside an "if (!IsPostBack)" condition should be justified. Of course, if you're handling control events you're in a PostBack. I mean each line of code placed in the common life cycle methods (Init, Load, PreRender, Render...). This methods will be invoked in every PostBack, so you must be really sure that this line is really needed out of a not IsPostBack condition, or you will be executing more than required, loosing performance and increasing bugs (more lines executed, more bugs).</p></blockquote>
<ul>
<li><strong>Don’t code for UI, code for Business.</strong> </li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>If you think on Databases design, this is obvious. Lets apply it to the other interface, the UI. I think there are two interfaces in distributed applications, the Database and the User Interface. Just before store data, and just before show it to the user, we usually need to format it to the appropriate data type (or format). But we should avoid to use a string to store a date value just because Database need it or the user prefers to see it in a different format. I mean, use always strongly typed entities and properties all along your application, and make the needed changes as late as possible. Doing this way, you don't have to worry about the correct format of the string containing a date value. In the other hand, think the same way parsing input data from the user, but doing it as soon as possible.</p></blockquote>
<ul>
<li><strong>Do the simple, do the best.</strong> </li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>Notepad never fails. Word never...., sorry, I can't lie. I can put lot of examples that demonstrate this tip, but I think this is the easy one. The really difficult thing is not to forget it.</p></blockquote>
<ul>
<li><strong>Read, learn and think.</strong> </li></ul>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>IT world is moving quickly and get changes each day. You don't have to be an expert on each new technology (I think is impossible), but you need to know something about where IT trends. Just knowing several ways you can choose the correct one. I think is more important to know what it could be done than the way it could. If you know something can be done, you can ask your colleagues, Google, forums, blogs... I'm sure you will finally find a solution. If you don't know something can be done.... you can't find the way.</p>
<p>As you can see, this is not a standard Best Practices article. Feel free commenting each tip, I hope you do it to get a better final version of this tips adding to them all your knowledge.</p>
<p>Thanks for the time.</p></blockquote><img src ="http://blogs.clearscreen.com/enadan/aggbug/2978.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Daniel de Andres</dc:creator><title>A DropDownList cannot have multiple items selected</title><link>http://blogs.clearscreen.com/enadan/archive/2006/02/13/2794.aspx</link><pubDate>Mon, 13 Feb 2006 09:58:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/02/13/2794.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2794.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/02/13/2794.aspx#Feedback</comments><slash:comments>19</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2794.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2794.aspx</trackback:ping><description>&lt;p&gt;This is something that made me crazy some months ago.&lt;/p&gt;
&lt;p&gt;As you probably know, DropDownList and ListBox have the same base class, ListControl, and both use ListItem to add Items to their collections. The difference between both controls is that DropDownList can have just one Item selected, while ListControl can have many Items selected at the same time.&lt;/p&gt;
&lt;p&gt;At runtime, you have two different ways to select items in both controls. You can set the Selected property of the ListItem you want to select to true, or set the SelectedIndex property of the controls to the index of the ListItem you want to select. Both are valid, but for the DropDownList the second one is better than the other because there are no checks to avoid multiple selection. If you have set more than one ListItem.Selected property to true, you'll get an Exception when the control is rendered.&lt;/p&gt;
&lt;p&gt;Conclusion.&lt;/p&gt;
&lt;p&gt;While you are working with DropDownList, try not to use the Selected property of the ListItems to change the selection of the control. If you want to select one Item, get the Item with one of this methods (ListItemCollection.FindByText(string Text) or ListItemCollection.FindByValue(string value)) and then set the SelectedIndex of the DropDownList to the result of the method ListItemCollection.IndexOf(ListItem item). Doing this way you won't have any problem.&lt;/p&gt;
&lt;p&gt;I hope you find this useful.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2794.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>This is something that made me crazy some months ago.</p>
<p>As you probably know, DropDownList and ListBox have the same base class, ListControl, and both use ListItem to add Items to their collections. The difference between both controls is that DropDownList can have just one Item selected, while ListControl can have many Items selected at the same time.</p>
<p>At runtime, you have two different ways to select items in both controls. You can set the Selected property of the ListItem you want to select to true, or set the SelectedIndex property of the controls to the index of the ListItem you want to select. Both are valid, but for the DropDownList the second one is better than the other because there are no checks to avoid multiple selection. If you have set more than one ListItem.Selected property to true, you'll get an Exception when the control is rendered.</p>
<p>Conclusion.</p>
<p>While you are working with DropDownList, try not to use the Selected property of the ListItems to change the selection of the control. If you want to select one Item, get the Item with one of this methods (ListItemCollection.FindByText(string Text) or ListItemCollection.FindByValue(string value)) and then set the SelectedIndex of the DropDownList to the result of the method ListItemCollection.IndexOf(ListItem item). Doing this way you won't have any problem.</p>
<p>I hope you find this useful.</p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2794.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Daniel de Andres</dc:creator><title>Web Forms are also Objects...</title><link>http://blogs.clearscreen.com/enadan/archive/2006/02/02/2747.aspx</link><pubDate>Thu, 02 Feb 2006 21:48:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/02/02/2747.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2747.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/02/02/2747.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2747.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2747.aspx</trackback:ping><description>&lt;p&gt;I know all of you are thinking that I'm stupid. Of course, everybody knows Web Forms are objects. But why, if everybody knows, not everybody thinks on it when developing web applications?!&lt;/p&gt;
&lt;p&gt;You can have the best Architecture, the best Data Access Layer, the best Business Rules and Objects.... what happens with pages?! Pages are always the worst part of any web applications I've ever seen. Coupling, modularization, abstraction, maintainability... are things that doesn't be cared in web pages.&lt;/p&gt;
&lt;p&gt;I know (and suffered each day) about client requirements. Windows designs tried to put directly into web pages that make us to code lots of lines in JavaScript. But really I think that this isn't the main problem. I think the problem is that when a page is finished, you can ask the developer who has coded about what happens if the user clicks here and the change this value and... he doesn't know.&lt;/p&gt;
&lt;p&gt;I think a way of improve Web Applications development is taking care about the design of the front-end. Pages are the only way users can scratch our code, because the front-end is where they can interact with the application. Is good to design "the perfect" Architecture and so on, but thinking that pages are just objects with some properties and some data showed in a different way depending on those properties.... I think the time spent developing the front-end and the number of bugs on it shouldn't be so higher.&lt;/p&gt;
&lt;p&gt;I'm writing an article about it but I'm not sure when it could be finished. Somethings included will be.&lt;/p&gt;
&lt;p&gt;- Page_Load always execute, even when the page won't be rendered again (in navigation, when you close it). Is this line really necessary out of the typical if (!IsPostBack)... ? Think about it and try to refactor it. &lt;/p&gt;
&lt;p&gt;- Try to design your pages as decoupled as you can from the rest of the application. This is even more important if the coupling is between pages.&lt;/p&gt;
&lt;p&gt;- Use private properties as flags to configure your object (your page). You can do things like this if you are waiting for that key from the previous page, or use ViewState if you just need this property during the postbacks.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;long&lt;/span&gt; theKeyINeedToLoadData 
{ 
 get 
 { 
  &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;if&lt;/span&gt; (Session[&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"theMagicKey"&lt;/span&gt;] !&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;null&lt;/span&gt;) 
  { 
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;return&lt;/span&gt; (&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;long&lt;/span&gt;)Session[&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"theMagicKey"&lt;/span&gt;]; 
  }
  &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;return&lt;/span&gt; -1; 
 } 
 
 set 
 { 
  Session[&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"theMagicKey"&lt;/span&gt;] &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; vale; 
 } 
} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;- Check this "really needed to work" properties as early as possible, and if you detect a problem stop execution. Show an error, redirect to some general page... but I'm sure at this point you already know something will goes wrong if you continue to the end of the life cycle.&lt;/p&gt;
&lt;p&gt;I hope I have enough time to finish this article, and then put it here to argue with all of you and learn together about it. &lt;/p&gt;
&lt;p&gt;Feel free commenting anything about this, I think is really important and interesting.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2747.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>I know all of you are thinking that I'm stupid. Of course, everybody knows Web Forms are objects. But why, if everybody knows, not everybody thinks on it when developing web applications?!</p>
<p>You can have the best Architecture, the best Data Access Layer, the best Business Rules and Objects.... what happens with pages?! Pages are always the worst part of any web applications I've ever seen. Coupling, modularization, abstraction, maintainability... are things that doesn't be cared in web pages.</p>
<p>I know (and suffered each day) about client requirements. Windows designs tried to put directly into web pages that make us to code lots of lines in JavaScript. But really I think that this isn't the main problem. I think the problem is that when a page is finished, you can ask the developer who has coded about what happens if the user clicks here and the change this value and... he doesn't know.</p>
<p>I think a way of improve Web Applications development is taking care about the design of the front-end. Pages are the only way users can scratch our code, because the front-end is where they can interact with the application. Is good to design "the perfect" Architecture and so on, but thinking that pages are just objects with some properties and some data showed in a different way depending on those properties.... I think the time spent developing the front-end and the number of bugs on it shouldn't be so higher.</p>
<p>I'm writing an article about it but I'm not sure when it could be finished. Somethings included will be.</p>
<p>- Page_Load always execute, even when the page won't be rendered again (in navigation, when you close it). Is this line really necessary out of the typical if (!IsPostBack)... ? Think about it and try to refactor it. </p>
<p>- Try to design your pages as decoupled as you can from the rest of the application. This is even more important if the coupling is between pages.</p>
<p>- Use private properties as flags to configure your object (your page). You can do things like this if you are waiting for that key from the previous page, or use ViewState if you just need this property during the postbacks.</p>
<p></p><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px"> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">private</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">long</span> theKeyINeedToLoadData 
{ 
 get 
 { 
  <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">if</span> (Session[<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"theMagicKey"</span>] !<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">null</span>) 
  { 
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">return</span> (<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">long</span>)Session[<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"theMagicKey"</span>]; 
  }
  <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">return</span> -1; 
 } 
 
 set 
 { 
  Session[<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"theMagicKey"</span>] <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> vale; 
 } 
} </span></pre>
<p>- Check this "really needed to work" properties as early as possible, and if you detect a problem stop execution. Show an error, redirect to some general page... but I'm sure at this point you already know something will goes wrong if you continue to the end of the life cycle.</p>
<p>I hope I have enough time to finish this article, and then put it here to argue with all of you and learn together about it. </p>
<p>Feel free commenting anything about this, I think is really important and interesting.</p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2747.aspx" width = "1" height = "1" /></body></item></channel></rss>