Thursday, April 3, 2014

Web User Controls in ASP.NET C#

Web User Controls are the newest process to add different parts of a website instead of Master Page concept. Here you can add the tools according to your format, and don't need to move in a same master page every time. Its a better approach to build a website than the master page. 

To do so you need to know about the Register, its an assembly  which will add reference of your controls into your page.

<%@ Register TagPrefix="gp" TagName="UCFooter"  Src="~/Controls/Footer.ascx" %>

Adding the source of control along with out own tag name and tag prefix.

But before that you need to add a new control into your solution explorer. For that click on Add a New Item and add Web User Control.



Now add your design into the design part of your .ascx file and write the code of it according to that one.

Now its time to link up your control to your page. Go to your page and add a register at the top of the code.

<%@ Register TagPrefix="gp" TagName="UCFooter"  Src="~/Controls/Footer.ascx" %>

And then using this add the control into your page like this.

<gp:UCFooter id="footer"  runat="server"  />

Now run your project it will show your page along with your control.



Here we have four parts into this page.
  1. Header (Control_Header)
  2. Menu (Control_Menu)
  3. Content (Page)
  4. Footer (Control_Footer)
So add these controls any where in any page and enjoy a better and more flexible method to build your site.

Download the full source code here.



0 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews