8.4 User Controls


8.4 User Controls

Like pages in ASP.NET, controls can also be defined mostly in code, mostly as tags on a page, or somewhere in between. So far, we have looked at defining controls entirely in code, but you can define something called a "user control" by using tags on a page as well. This is very convenient for composite controls, because it allows you to lay out your control's HTML tags on a page rather than programmatically.

To define a user control, you create a page with an .ascx extension and use the @Control directive where you would normally use an @Page directive in a standard .aspx page. The @Control directive takes the same attributes as the @Page directive. You then lay out the controls you want to appear in your user control, making sure not to include html , body , or form tags, because these will be supplied by the client. You can also add properties and events in a server-side script block. Listing 8-34 shows our calculator control rewritten as a user control. Notice that properties and methods are declared within the server-side script block as if we were inside our control class definition. This .ascx page will be compiled into a distinct control class when referenced by a client .aspx page.

Listing 8-34 A User Control
 <%@ Control Description="A simple calculator" %> <asp:TextBox ID="Op1" runat=server/> + <asp:TextBox ID="Op2" runat=server/> = <asp:TextBox ID="Res" runat=server/> <br/> <asp:Button Text="Calculate"             OnClick="OnCalculate" runat=server/> <script language="C#" runat=server>   private void OnCalculate(Object sender, EventArgs e)   {     int res = Convert.ToInt32(Op1.Text) +               Convert.ToInt32(Op2.Text);     Res.Text = res.ToString();   }   public int Result   {     get { return Convert.ToInt32(Res.Text); }   } </script> 

User controls can be accessed from any .aspx page much like other custom controls. The one major difference is that the .ascx file must be accessible by the client page, and it must be referenced using the Src attribute of the @Register directive. If you need to load a user control dynamically, you can use the LoadControl method of the Page class, which takes the file name of the user control. When a user control is loaded programmatically, the name of the new control class is the name of the file containing the user control, replacing the "." with an underscore (_). For example, a user control written in file Foo.ascx would generate a new control class of type Foo_ascx when loaded with LoadControl . Listing 8-35 shows an example of an .aspx page that references two user controls. The first one, user1 , is loaded using the @Register directive. The second, user2 , is loaded programmatically using the LoadControl method of the Page class.

Listing 8-35 A Sample Client to a User Control
 <%@ Page Language="C#" %> <%@ Register TagPrefix="uc1" TagName="UserControl1"     Src="UserControl1.ascx" %> <html>   <script runat=server>   void Page_Load(Object sender, EventArgs e)   {     Control uc2 = LoadControl("UserControl2.ascx");     Page.Controls.Add(uc2);   }   </script> <body> <form runat=server>   <uc1:UserControl1 id="UC1" runat=server/> </form> </body> </html> 


Essential ASP.NET With Examples in C#
Essential ASP.NET With Examples in C#
ISBN: 0201760401
EAN: 2147483647
Year: 2003
Pages: 94
Authors: Fritz Onion

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net