In this post we will discuss how to create and use user control in Asp.Net. Also we will discuss how we can create a website in asp.net. Also you can check out my previous posts on:
– ABC of WCF
Here we will discuss how to create a web site. To create a web site Open Visual Studio 2010 Go to File -> New WebSite.
Then from the New Web Site template dialog box Choose Asp.Net WebSite. Then Give the path and click on OK.
Now since our web site is ready, we will now see how to create a user control. To create a user control Right click on the project then Click on Add New Item. Then from the Add New Item dialog box
Choose Web User Control, give a Name and click on Add. Then in the acsx page you can add the controls that you want. You can see the details on the attached project which you can download from the below.
Now since we have finished creating the user control, we will discuss how to add this to the page.
To add a user control to a page, we have to use the Register tag of asp.net. We have to write like below:
<%@ Register src=”WebUserControl.ascx” tagname=”WebUserControl” tagprefix=”uc1″ %>
where src is the path of the user control and tagname and tagprefix are used to call the web user control.
Now to call the user control write like below:
<TagPrefix:TagName ID:”ID” runat=”server”/>. So it will be like below:
<uc1:WebUserControl ID=”WebUserControl1″ runat=”server” />
There is also another approach to call or use user conrol. What we need to do is simply drag and drop the user control to the design of the page.