ANOTHER BONUS TIP! Form Fields and Events in ASP.NET 2.0

Posted Monday, April 18, 2005 2:27 PM by C-Dog's .NET Tip of the Day
In the past when you created a new textbox, label, dropdownlist, or whatever in an ASP.NET page or control, you had to go into design view to make the variable declaration show up in the code behind.  Then sometimes your form would be hosed and Visual Studio wouldn't bother to create the variable declaration.  The same thing was true with events.  The easiest way to create an event was to double click on a button, etc to get the event created.
 
.NET 2.0 introduces the concept of a partial class.  The code behind page has the new partial modifier on the class declaration.  What this means is anything declared in the markup (form fields, etc.) does not have to have a matching reference in the code behind.  Simply adding the form field to the markup (design view not necessary) is enough to make Intellisense find the object and allow you to access it in the code behind.
 
Events work the same way.  I am sure you have seen lines like this in your InitialzeComponent method.
 
this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click);
 
Now whenever you want to bind a click event to a button, you can simply add OnClick as one of the attributes to the form field, such as.
 
<asp:button id="MyButton" OnClick="MyButton_Click" runat="server" Text="Blah" />

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=142