<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#: ASP.NET</title><link>http://blogs.clearscreen.com/enadan/category/161.aspx</link><description>Just funny things happened to me in my daily work and how to fix it. </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 2.0 Tips &amp; Tricks</title><link>http://blogs.clearscreen.com/enadan/archive/2006/04/25/3013.aspx</link><pubDate>Tue, 25 Apr 2006 18:20:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/04/25/3013.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/3013.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/04/25/3013.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/3013.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/3013.aspx</trackback:ping><description>&lt;p&gt;An article from &lt;a href="http://weblogs.asp.net/scottgu/"&gt;ScottGu's Blog&lt;/a&gt;, where you can download some slides and samples about new things in ASP.NET 2.0 that make web development easier. I've seen the slides and I've found them really interesting.... but I can't enjoy the samples yet (I don't have enought time).&lt;/p&gt;
&lt;p&gt;The article: &lt;a href="http://weblogs.asp.net/scottgu/archive/2006/04/03/441787.aspx"&gt;ASP.NET Connections Tips and Tricks Slides+Demos Posted&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Hope you enjoy.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/3013.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>An article from <a href="http://weblogs.asp.net/scottgu/">ScottGu's Blog</a>, where you can download some slides and samples about new things in ASP.NET 2.0 that make web development easier. I've seen the slides and I've found them really interesting.... but I can't enjoy the samples yet (I don't have enought time).</p>
<p>The article: <a href="http://weblogs.asp.net/scottgu/archive/2006/04/03/441787.aspx">ASP.NET Connections Tips and Tricks Slides+Demos Posted</a> </p>
<p>Hope you enjoy.</p><img src ="http://blogs.clearscreen.com/enadan/aggbug/3013.aspx" width = "1" height = "1" /></body></item><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>Compilation Error executing ASP.NET Page.</title><link>http://blogs.clearscreen.com/enadan/archive/2006/03/23/2953.aspx</link><pubDate>Thu, 23 Mar 2006 17:11:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/03/23/2953.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2953.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/03/23/2953.aspx#Feedback</comments><slash:comments>13</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2953.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2953.aspx</trackback:ping><description>&lt;p&gt;I know the title is strange. Compilation error trying to execute a page? Well, continue reading and you will understand.&lt;/p&gt;
&lt;p&gt;You know all languages in .NET are OOP languages, so pages in ASP.NET are also objects. When you compile an ASP.NET project, you just compile the code-behind code but, what happens with the *.aspx files? The code in this files is parsed to several *.cs files (one per page) and compiled on demand.&lt;/p&gt;
&lt;p&gt;Trying to give a funny speech to my team about ASP.NET best practices, defensive programming, standards..., I began to play with a page and place a default constructor on the code behind. Nothing happens. Ok, is a class and it could have a constructor. You really can't do something useful here, but you can code it. &lt;/p&gt;
&lt;p&gt;Try again with a private constructor. Compile the project and execute. Here you have the "Compilation Error executing ASP.NET Page":&lt;/p&gt;
&lt;p&gt;Compiler Error Message: CS0122: 'DemoCharla.AspxWithConstructor.AspxWithConstructor()' is inaccessible due to its protection level.&lt;/p&gt;
&lt;p&gt;As you know, in the @Page directive in *.aspx file you specify Inherits attribute. This really means that the *.aspx files inherits code-behind classes, but obviously, after parsing them to corresponding *.cs files and compiling. &lt;/p&gt;
&lt;p&gt;Just one useful thing is that you can see the source code of the generated *.cs file in the error page, clicking in the "Show Complete Compilation Source" link This is the same as you can see in the "Temporary ASP.NET Files" folder behind \Framework\&amp;lt; version &amp;gt;\ folder. If you go there and look inside your folder project, you will find a folder with a name like 5e170680 (this name is always different, but have the same format) and inside this folder you will find another folder with similar name. Here is where all the temporary files are generated. You can see *.cs files, *.cmdfiles which contains necessary parameters for compilation, the *.out and *.err as a result of this compilation and more.&lt;/p&gt;
&lt;p&gt;You can try and enjoy for a while seeing all this files, and executing a page and seeing csc.exe in the process monitor compiling pages for the first time. Isn't really useful but is what really happens "behind the scenes".&lt;/p&gt;
&lt;p&gt;There's a good article in &lt;a href="http://www.codeproject.com"&gt;Code Project &lt;/a&gt;from Natty Gur: &lt;a href="http://www.codeproject.com/aspnet/ASPXFILES.asp"&gt;http://www.codeproject.com/aspnet/ASPXFILES.asp&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt; &lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2953.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>I know the title is strange. Compilation error trying to execute a page? Well, continue reading and you will understand.</p>
<p>You know all languages in .NET are OOP languages, so pages in ASP.NET are also objects. When you compile an ASP.NET project, you just compile the code-behind code but, what happens with the *.aspx files? The code in this files is parsed to several *.cs files (one per page) and compiled on demand.</p>
<p>Trying to give a funny speech to my team about ASP.NET best practices, defensive programming, standards..., I began to play with a page and place a default constructor on the code behind. Nothing happens. Ok, is a class and it could have a constructor. You really can't do something useful here, but you can code it. </p>
<p>Try again with a private constructor. Compile the project and execute. Here you have the "Compilation Error executing ASP.NET Page":</p>
<p>Compiler Error Message: CS0122: 'DemoCharla.AspxWithConstructor.AspxWithConstructor()' is inaccessible due to its protection level.</p>
<p>As you know, in the @Page directive in *.aspx file you specify Inherits attribute. This really means that the *.aspx files inherits code-behind classes, but obviously, after parsing them to corresponding *.cs files and compiling. </p>
<p>Just one useful thing is that you can see the source code of the generated *.cs file in the error page, clicking in the "Show Complete Compilation Source" link This is the same as you can see in the "Temporary ASP.NET Files" folder behind \Framework\&lt; version &gt;\ folder. If you go there and look inside your folder project, you will find a folder with a name like 5e170680 (this name is always different, but have the same format) and inside this folder you will find another folder with similar name. Here is where all the temporary files are generated. You can see *.cs files, *.cmdfiles which contains necessary parameters for compilation, the *.out and *.err as a result of this compilation and more.</p>
<p>You can try and enjoy for a while seeing all this files, and executing a page and seeing csc.exe in the process monitor compiling pages for the first time. Isn't really useful but is what really happens "behind the scenes".</p>
<p>There's a good article in <a href="http://www.codeproject.com">Code Project </a>from Natty Gur: <a href="http://www.codeproject.com/aspnet/ASPXFILES.asp">http://www.codeproject.com/aspnet/ASPXFILES.asp</a></p>
<p><br /> </p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2953.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Daniel de Andres</dc:creator><title>ListItemCollection.Insert() is really dangerous....</title><link>http://blogs.clearscreen.com/enadan/archive/2006/03/07/2902.aspx</link><pubDate>Tue, 07 Mar 2006 17:21:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/03/07/2902.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2902.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/03/07/2902.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2902.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2902.aspx</trackback:ping><description>&lt;p&gt;Hi again.&lt;/p&gt;
&lt;p&gt;In all the web controls that inherits from System.Web.UI.WebControls.ListControl (DropDownList, ListBox...) we have 2 ways to add items in runtime:&lt;/p&gt;
&lt;p&gt;- TheControl.Items.Add(ListItem item);&lt;br /&gt;- TheControl.Items.Insert(int index, ListItem item);&lt;/p&gt;
&lt;p&gt;I'd found a funny thing using the second one. If you try to add a null Item using the first one, we get an Exception just in that line, but using the second one we also get an exception but now when the page is rendering. ¿Why both are diferent? I don't know but it can be a problem because you can't catch the exception in the appropiate place. Using Insert and adding a null Item the SelectedIndex property is also null (if there are no items selected it should be -1), so trying to access it will throw an Exception also.&lt;/p&gt;
&lt;p&gt;Ok, I know nobody will do this, at least not directly ( Items.Insert(0, null) ), but I think it wouldn't be bad practicing defensive programing here. Perhaps you have coded a method that returns a ListItem, so in this case be sure to check if the returning ListItem is not null. &lt;/p&gt;
&lt;p&gt;Isn't really a technical bug (perhaps design), and if it happens is really easy to detect, but I post it here because it makes me laugh for a while.&lt;/p&gt;
&lt;p&gt;Nothing else today.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2902.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>Hi again.</p>
<p>In all the web controls that inherits from System.Web.UI.WebControls.ListControl (DropDownList, ListBox...) we have 2 ways to add items in runtime:</p>
<p>- TheControl.Items.Add(ListItem item);<br />- TheControl.Items.Insert(int index, ListItem item);</p>
<p>I'd found a funny thing using the second one. If you try to add a null Item using the first one, we get an Exception just in that line, but using the second one we also get an exception but now when the page is rendering. ¿Why both are diferent? I don't know but it can be a problem because you can't catch the exception in the appropiate place. Using Insert and adding a null Item the SelectedIndex property is also null (if there are no items selected it should be -1), so trying to access it will throw an Exception also.</p>
<p>Ok, I know nobody will do this, at least not directly ( Items.Insert(0, null) ), but I think it wouldn't be bad practicing defensive programing here. Perhaps you have coded a method that returns a ListItem, so in this case be sure to check if the returning ListItem is not null. </p>
<p>Isn't really a technical bug (perhaps design), and if it happens is really easy to detect, but I post it here because it makes me laugh for a while.</p>
<p>Nothing else today.</p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2902.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Daniel de Andres</dc:creator><title>UserControl.IsPostBack vs Page.IsPostBack</title><link>http://blogs.clearscreen.com/enadan/archive/2006/02/21/2838.aspx</link><pubDate>Tue, 21 Feb 2006 18:29:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/02/21/2838.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2838.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/02/21/2838.aspx#Feedback</comments><slash:comments>14</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2838.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2838.aspx</trackback:ping><description>&lt;p&gt;No one wins, both are the same :-(. &lt;/p&gt;
&lt;p&gt;In the project I'm currently working, we need a real UserControl.IsPostBack property, so we had to create a new one in our CustomControlsBase class, named IsUserControlPostBack. This new property is really useful because we need to avoid database round trips if data is already loaded in the control and so on (just the same like Page.IsPostBack, but inside a UserControl).&lt;/p&gt;
&lt;p&gt;Some lines of code will explain better what we did to implement it.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;&lt;font color="#008000"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//ReadOnly property.&lt;/span&gt;&lt;/font&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt;&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt; &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;bool&lt;/span&gt;&lt;/font&gt; IsUserControlPostBack
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;{
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;&lt;font color="blue"&gt;    get
&lt;/font&gt;&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;    {
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;        &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt;&lt;/font&gt; &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;&lt;/font&gt;.ViewState[&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"IsUserControlPostBa&lt;wbr&gt;ck"&lt;/wbr&gt;&lt;/span&gt;] !&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;&lt;/font&gt;;
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;    }
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;}&lt;br /&gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;&lt;br /&gt;&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;
&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;&lt;font color="#008000"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//Initializing the new variable updating ViewState data.&lt;/span&gt;&lt;/font&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt;&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt; &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt;&lt;/font&gt; &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt;&lt;/font&gt; OnLoad(System.EventArgs e)
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;{
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;    &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;&lt;/font&gt;.OnLoad (e);&lt;/font&gt;

&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;    &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt;&lt;/font&gt; (!IsUserControlPostBack)
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;    {
        &lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;&lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;&lt;/font&gt;.ViewState.Add(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"IsUserControlPo&lt;wbr&gt;stBack"&lt;/wbr&gt;&lt;/span&gt;, &lt;font color="blue"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;true&lt;/span&gt;&lt;/font&gt;);
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;    }
&lt;/font&gt;&lt;font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&amp;lt;span"&gt;}&lt;/font&gt;&lt;/font&gt;
&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Doing this way, we are always sure about when the UserControl is loaded due to a client PostBack, and when for the first time. I think it's a good use of the ViewState. &lt;/p&gt;
&lt;p&gt;I hope you find this useful.&lt;/p&gt;
&lt;p&gt;--------------------- Added to explain why I find it useful. Thanks ADI for your comments ------------------&lt;/p&gt;
&lt;p&gt;This property wouldn't be useful is you are loading your usercontrol at the same time you load the page but, if you want to load a usercontrol after the page is loaded, the Page.IsPostBack will be always true, so you can't know if the usercontrol is loaded for first time or not. Think on loading usercontrols if the user clicks on a button for example. &lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2838.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>No one wins, both are the same :-(. </p>
<p>In the project I'm currently working, we need a real UserControl.IsPostBack property, so we had to create a new one in our CustomControlsBase class, named IsUserControlPostBack. This new property is really useful because we need to avoid database round trips if data is already loaded in the control and so on (just the same like Page.IsPostBack, but inside a UserControl).</p>
<p>Some lines of code will explain better what we did to implement it.</p>
<p></p><pre><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"><font color="#008000"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//ReadOnly property.</span></font>
<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span></font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"> <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">bool</span></font> IsUserControlPostBack
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">{
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"><font color="blue">    get
</font></font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">    {
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">        <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span></font> <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">this</span></font>.ViewState[<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"IsUserControlPostBa<wbr>ck"</wbr></span>] !<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span></font>;
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">    }
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">}<br /></font></pre><pre><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"><br /></font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">
<font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"><font color="#008000"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//Initializing the new variable updating ViewState data.</span></font>
<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span></font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"> <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span></font> <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span></font> OnLoad(System.EventArgs e)
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">{
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">    <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span></font>.OnLoad (e);</font>

<font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">    <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span></font> (!IsUserControlPostBack)
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">    {
        </font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span"><font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">this</span></font>.ViewState.Add(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"IsUserControlPo<wbr>stBack"</wbr></span>, <font color="blue"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">true</span></font>);
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">    }
</font><font style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4" face="&lt;span">}</font></font>
</pre>
<p></p>
<p>Doing this way, we are always sure about when the UserControl is loaded due to a client PostBack, and when for the first time. I think it's a good use of the ViewState. </p>
<p>I hope you find this useful.</p>
<p>--------------------- Added to explain why I find it useful. Thanks ADI for your comments ------------------</p>
<p>This property wouldn't be useful is you are loading your usercontrol at the same time you load the page but, if you want to load a usercontrol after the page is loaded, the Page.IsPostBack will be always true, so you can't know if the usercontrol is loaded for first time or not. Think on loading usercontrols if the user clicks on a button for example. </p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2838.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>Hijacking __doPostBack: using the JavaScript interception pattern</title><link>http://blogs.clearscreen.com/enadan/archive/2006/02/12/2791.aspx</link><pubDate>Sun, 12 Feb 2006 12:42:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/02/12/2791.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2791.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/02/12/2791.aspx#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2791.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2791.aspx</trackback:ping><description>&lt;p&gt;Hi all. &lt;br /&gt;I will try to explain how to use the JavaScript interception pattern and why I need to do it in my project to hijack the __doPostBack function.&lt;/p&gt;
&lt;p&gt;I had to detect if user had made changes in some pages of the web application and then ask him to Save Changes. Of course, due to client requirements and performances issues I had to code it in client side, so I put some hiddens in my Base Page and code some scripts to change their values (is so complicated to explain so I won't explain this at all). I will focus on two of the functions. One of them set the value of one hidden (hiddenHasChanged) that tell me if page has been changed to true, and the other evaluate this hidden and if it's value is true, ask the user for Save Changes and store the response of the confirm dialog in other hidden (hiddenSaveChanges). Then I check the value of the hiddenSaveChanges in server side and decide to save changes or not.&lt;/p&gt;
&lt;p&gt;To modify hiddenHasChanged I add some attributes to the controls in our custom controls library ("onchange", "SetHiddenHasChanged();"). Now I know if Page has been changed at any time but.... where can I place the code that will invoke the confirm dialog? I decide to subscribe to the onunload event and invoke it there. It looks like this:&lt;/p&gt;
&lt;p&gt;&amp;lt; script&amp;gt;&lt;br /&gt;    onunload = AskUser; &lt;br /&gt;    function SetHiddenHasChanged() &lt;br /&gt;    { &lt;br /&gt;        document.getElementById('hiddenHasChanged').value = true; &lt;br /&gt;    } &lt;br /&gt;    function AskUser() &lt;br /&gt;    { &lt;br /&gt;        if (document.getElementById('hiddenHasChanged').value )&lt;br /&gt;        document.getElementById('hiddenSaveChanges').value = (confirm('Do you want to save changes?')); &lt;br /&gt;    }&lt;br /&gt;&amp;lt; /script&amp;gt;&lt;/p&gt;
&lt;p&gt;When I began to test it I noticed that even the value of the hiddenSaveChanges before the PostBack were the result of the confirm dialog, when I got it in Server Side code this value wasn't updated. What was happened? The new value was missing and I didn't know where. Finally I got the conclusion that, although the value was correctly updated in client side, the data sent by the request was collected before my script code was executed.&lt;/p&gt;
&lt;p&gt;How can I fix this?&lt;/p&gt;
&lt;p&gt;I discard using JavaScript events like onunload, onbeforeunload... no one goes right for me. I need to hijack the __doPostBack function and place me code just before the form submission. So I place this script code in my Base Class. Notice you can't place this code in the &amp;lt;Head&amp;gt; section because __doPostBack doesn't exist yet, so I have place it just before the close of the &amp;lt;Form&amp;gt; tag.&lt;/p&gt;
&lt;p&gt;&amp;lt; script&amp;gt;&lt;br /&gt;    if (typeof(__doPostBack) == "function")&lt;br /&gt;    {&lt;br /&gt;        var __doPostBackOLD = __doPostBack; &lt;br /&gt;        __doPostBack = function(eventTarget, eventArgument)&lt;br /&gt;       {&lt;br /&gt;           ModifyHidden(confirm('Do you want to save changes?'));&lt;br /&gt;           __doPostBackOLD(eventTarget, eventArgument);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&amp;lt; /script&amp;gt; &lt;/p&gt;
&lt;p&gt;Doing this way I'd fix the problem. I've seen this pattern used with Page_ClientValidate function, and even with __doPostBack but never to modify data, just to avoid the submission of the form when some data is wrong, or to show a "Please Wait" message during PostBacks. This pattern obviously is not new, but perhaps the use of it.&lt;/p&gt;
&lt;p&gt;I hope you find it useful. What do you think about?&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2791.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>Hi all. <br />I will try to explain how to use the JavaScript interception pattern and why I need to do it in my project to hijack the __doPostBack function.</p>
<p>I had to detect if user had made changes in some pages of the web application and then ask him to Save Changes. Of course, due to client requirements and performances issues I had to code it in client side, so I put some hiddens in my Base Page and code some scripts to change their values (is so complicated to explain so I won't explain this at all). I will focus on two of the functions. One of them set the value of one hidden (hiddenHasChanged) that tell me if page has been changed to true, and the other evaluate this hidden and if it's value is true, ask the user for Save Changes and store the response of the confirm dialog in other hidden (hiddenSaveChanges). Then I check the value of the hiddenSaveChanges in server side and decide to save changes or not.</p>
<p>To modify hiddenHasChanged I add some attributes to the controls in our custom controls library ("onchange", "SetHiddenHasChanged();"). Now I know if Page has been changed at any time but.... where can I place the code that will invoke the confirm dialog? I decide to subscribe to the onunload event and invoke it there. It looks like this:</p>
<p>&lt; script&gt;<br />    onunload = AskUser; <br />    function SetHiddenHasChanged() <br />    { <br />        document.getElementById('hiddenHasChanged').value = true; <br />    } <br />    function AskUser() <br />    { <br />        if (document.getElementById('hiddenHasChanged').value )<br />        document.getElementById('hiddenSaveChanges').value = (confirm('Do you want to save changes?')); <br />    }<br />&lt; /script&gt;</p>
<p>When I began to test it I noticed that even the value of the hiddenSaveChanges before the PostBack were the result of the confirm dialog, when I got it in Server Side code this value wasn't updated. What was happened? The new value was missing and I didn't know where. Finally I got the conclusion that, although the value was correctly updated in client side, the data sent by the request was collected before my script code was executed.</p>
<p>How can I fix this?</p>
<p>I discard using JavaScript events like onunload, onbeforeunload... no one goes right for me. I need to hijack the __doPostBack function and place me code just before the form submission. So I place this script code in my Base Class. Notice you can't place this code in the &lt;Head&gt; section because __doPostBack doesn't exist yet, so I have place it just before the close of the &lt;Form&gt; tag.</p>
<p>&lt; script&gt;<br />    if (typeof(__doPostBack) == "function")<br />    {<br />        var __doPostBackOLD = __doPostBack; <br />        __doPostBack = function(eventTarget, eventArgument)<br />       {<br />           ModifyHidden(confirm('Do you want to save changes?'));<br />           __doPostBackOLD(eventTarget, eventArgument);<br />        }<br />    }<br />&lt; /script&gt; </p>
<p>Doing this way I'd fix the problem. I've seen this pattern used with Page_ClientValidate function, and even with __doPostBack but never to modify data, just to avoid the submission of the form when some data is wrong, or to show a "Please Wait" message during PostBacks. This pattern obviously is not new, but perhaps the use of it.</p>
<p>I hope you find it useful. What do you think about?</p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2791.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Daniel de Andres</dc:creator><title>Defensive programming in Custom Controls</title><link>http://blogs.clearscreen.com/enadan/archive/2006/02/05/2757.aspx</link><pubDate>Sun, 05 Feb 2006 21:00:00 GMT</pubDate><guid>http://blogs.clearscreen.com/enadan/archive/2006/02/05/2757.aspx</guid><wfw:comment>http://blogs.clearscreen.com/enadan/comments/2757.aspx</wfw:comment><comments>http://blogs.clearscreen.com/enadan/archive/2006/02/05/2757.aspx#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/enadan/comments/commentRss/2757.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/enadan/services/trackbacks/2757.aspx</trackback:ping><description>&lt;p&gt;One of the Best Practice in ASP.NET projects is to develop a Custom Controls Library to encapsulate and centralize all the common code and custom features we want in our application. All changes made to a control will affect all the pages having this control, so we earn time and detect bugs more efficiently.&lt;/p&gt;
&lt;p&gt;Of course, in the project I'm currently working we have all the UI Controls as Custom Controls, but we have had some problems. In some of the Custom Controls, we had to add some client logic in JavaScript. We did it adding javascript handlers for some client events of the controls, as "onclick", "onblur" and so on. The problem is that we did it in the Page.PreRender time, and we override the previous attributes added in some pages. I think is better to me if I explain it with some lines of code (I'm sorry but my english is not as good as I need now...)&lt;/p&gt;
&lt;p&gt;1.- Adding attributes to handle client events. This code belongs to a Custom TextBox.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; OnPreRender(&lt;/font&gt;&lt;font color="#008080" size="2"&gt;EventArgs&lt;/font&gt;&lt;font size="2"&gt; e)
{
    &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//more code&lt;/span&gt;
    this.Attributes.Add(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"onchange"&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#800000" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"ChangeForeColor();"&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;);
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;.OnPreRender(e);
}&lt;/font&gt;

&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2.- Use of the Custom TextBox in a Page.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; Page_Load(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; sender, &lt;/font&gt;&lt;font color="#008080" size="2"&gt;EventArgs&lt;/font&gt;&lt;font size="2"&gt; e)
{
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;    //more code&lt;/span&gt;
    txtOne.Attributes.Add(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"onchange"&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#800000" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"ValidateInput();"&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;);
}
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;3.- Explanation.&lt;/p&gt;
&lt;p&gt;Now I'll try to explain it. In the first piece of code, we add an handler to the "onchange" event of the textbox that will change the color of the text on it. This behavior is requiered in all the application, so we placed it in our custom TextBox. One of the developers, need to add another handler to the "onchange" event to validate the input. He was new in the project and he didn't know all the Custom Controls behaviors yet, so he did what anyelse would do: put in his page the second piece of code. Of course, the handler of the developer never was executed. He asked us and we saw the problem. We were overriding his code!!&lt;/p&gt;
&lt;p&gt;4.- The solution. We add this method to the Custom TextBox to avoid this problem. Prior adding our handlers to an attribute, now we check if there's any handler added by a developer and we always add all of them. We will never override developers code again!&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; OnPreRender(&lt;/font&gt;&lt;font color="#008080" size="2"&gt;EventArgs&lt;/font&gt;&lt;font size="2"&gt; e)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//more code&lt;/span&gt;&lt;br /&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//Old Code &lt;br /&gt;&lt;/span&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//&lt;/span&gt;&lt;font color="#008000"&gt;&lt;font color="#008000"&gt;this&lt;/font&gt;.Attributes.Add(&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&lt;font size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"onchange"&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"ChangeForeColor();"&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;);&lt;br /&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//New Code &lt;br /&gt;&lt;/span&gt;&lt;font color="#000000"&gt;    AddAttributeSafely(&lt;/font&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;&lt;font color="#000000"&gt;"o&lt;/font&gt;&lt;font color="#000000"&gt;nchange"&lt;/font&gt;&lt;/span&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"ChangeForeColor();"&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;);&lt;/font&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#0000ff" size="2"&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;.OnPreRender(e);&lt;br /&gt;}&lt;/font&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; AddAttributeSafely(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; attributeType, &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; attributeValue)
{
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; (attributeType.Length == 0 &amp;amp;&amp;amp; attributeValue.Length == 0)
    {
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;        &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;;
    }

&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; (Attributes[attributeType] == &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; || Attributes[attributeType].Length == 0)
    {
        Attributes.Add(attributeType, attributeValue);
    }
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;    &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;else&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;
    {
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;        &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; existingAttribute &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Attributes[attributeType];
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;        &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt; (existingAttribute.IndexOf(attributeValue) == -1)
        {
            Attributes.Add(attributeType, existingAttribute &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;+&lt;/span&gt; attributeValue);
        }
    }
}
&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;I hope you find this useful in your projects.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/enadan/aggbug/2757.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>One of the Best Practice in ASP.NET projects is to develop a Custom Controls Library to encapsulate and centralize all the common code and custom features we want in our application. All changes made to a control will affect all the pages having this control, so we earn time and detect bugs more efficiently.</p>
<p>Of course, in the project I'm currently working we have all the UI Controls as Custom Controls, but we have had some problems. In some of the Custom Controls, we had to add some client logic in JavaScript. We did it adding javascript handlers for some client events of the controls, as "onclick", "onblur" and so on. The problem is that we did it in the Page.PreRender time, and we override the previous attributes added in some pages. I think is better to me if I explain it with some lines of code (I'm sorry but my english is not as good as I need now...)</p>
<p>1.- Adding attributes to handle client events. This code belongs to a Custom TextBox.</p>
<p></p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span></font><font size="2"> </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span></font><font size="2"> </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span></font><font size="2"> OnPreRender(</font><font color="#008080" size="2">EventArgs</font><font size="2"> e)
{
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//more code</span>
    this.Attributes.Add(</font><font color="#800000" size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"onchange"</span></font><font size="2">, </font><font color="#800000" size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"ChangeForeColor();"</span></font><font size="2">);
</font><font color="#0000ff" size="2">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span></font><font size="2">.OnPreRender(e);
}</font>

</span></pre>
<p></p>
<p>2.- Use of the Custom TextBox in a Page.</p>
<p></p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font size="2"><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span></font><font size="2"> </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span></font><font size="2"> Page_Load(</font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span></font><font size="2"> sender, </font><font color="#008080" size="2">EventArgs</font><font size="2"> e)
{
<span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">    //more code</span>
    txtOne.Attributes.Add(</font><font color="#800000" size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"onchange"</span></font><font size="2">, </font><font color="#800000" size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"ValidateInput();"</span></font><font size="2">);
}
</font></font></span></pre>
<p>3.- Explanation.</p>
<p>Now I'll try to explain it. In the first piece of code, we add an handler to the "onchange" event of the textbox that will change the color of the text on it. This behavior is requiered in all the application, so we placed it in our custom TextBox. One of the developers, need to add another handler to the "onchange" event to validate the input. He was new in the project and he didn't know all the Custom Controls behaviors yet, so he did what anyelse would do: put in his page the second piece of code. Of course, the handler of the developer never was executed. He asked us and we saw the problem. We were overriding his code!!</p>
<p>4.- The solution. We add this method to the Custom TextBox to avoid this problem. Prior adding our handlers to an attribute, now we check if there's any handler added by a developer and we always add all of them. We will never override developers code again!</p>
<p><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span></font><font size="2"> </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span></font><font size="2"> </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span></font><font size="2"> OnPreRender(</font><font color="#008080" size="2">EventArgs</font><font size="2"> e)<br />{<br />    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//more code</span><br />    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//Old Code <br /></span>    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//</span><font color="#008000"><font color="#008000">this</font>.Attributes.Add(</font></font><font color="#008000"><font size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"onchange"</span></font><font size="2">, </font><font size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"ChangeForeColor();"</span></font></font><font color="#008000" size="2">);<br />    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//New Code <br /></span><font color="#000000">    AddAttributeSafely(</font><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"><font color="#000000">"o</font><font color="#000000">nchange"</font></span><font size="2">, </font><font size="2"><span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"ChangeForeColor();"</span></font><font size="2">);</font></font><br /><font color="#0000ff" size="2">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span></font><font size="2">.OnPreRender(e);<br />}</font><br /></span></p>
<p></p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span></font><font size="2"> </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span></font><font size="2"> AddAttributeSafely(</font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span></font><font size="2"> attributeType, </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span></font><font size="2"> attributeValue)
{
</font><font color="#0000ff" size="2">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span></font><font size="2"> (attributeType.Length == 0 &amp;&amp; attributeValue.Length == 0)
    {
</font><font color="#0000ff" size="2">        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span></font><font size="2">;
    }

</font><font color="#0000ff" size="2">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span></font><font size="2"> (Attributes[attributeType] == </font><font color="#0000ff" size="2"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span></font><font size="2"> || Attributes[attributeType].Length == 0)
    {
        Attributes.Add(attributeType, attributeValue);
    }
</font><font color="#0000ff" size="2">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">else</span></font><font size="2">
    {
</font><font color="#0000ff" size="2">        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span></font><font size="2"> existingAttribute <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Attributes[attributeType];
</font><font color="#0000ff" size="2">        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span></font><font size="2"> (existingAttribute.IndexOf(attributeValue) == -1)
        {
            Attributes.Add(attributeType, existingAttribute <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">+</span> attributeValue);
        }
    }
}
</font></span></pre>
<p>I hope you find this useful in your projects.</p><img src ="http://blogs.clearscreen.com/enadan/aggbug/2757.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>