This tutorial will demonstrate how to add a Web User Control to a Web Form using both ASP.NET 4.0 and C#.
Web User Controls can be used in ASP.NET to allow developers to create complex controls and reuse them in multiple places. For instance, if we were making a website in which you needed a particular form in more than one place, you could add the form to the Web User Control and then add that to multiple different pages easily.
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Creating a Web User Control
At this point in the tutorial I have created a new ASP.NET Empty Web Site. What we need to do first is add a new Web User Control to the project, to do this:
- Right click the project in your Solution Explorer.
- Select Add New Item...
- Select a Web User Control.
- Name it ContactForm.ascx.
- Click Add.
Next, we need to add some controls onto the Web User Control. Because we are not going to create any sort of functionality in here, I'm going to keep it simple and just add in a TextBox and a Button. To do this, open ContactForm.ascx up to Design mode and:
- Drag and drop a TextBox onto the Web User Control.
- To the right of the TextBox hit Enter to create a line break.
- Drag and drop a Button onto the Web User Control under the TextBox.
Adding the Web Form
Next, we need to add a Web Form to the project that we will add our Web User Control to. To do this:
- Right click the project in your Solution Explorer.
- Select Add New Item...
- Select a Web Form.
- Name it Default.aspx.
- Click Add.
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
What we are going to do now is demonstrate two different ways to add the Web User Control to the Web Form. First, to add it in ASP open the Default.aspx up to Source mode and:
- At the top of the file, underneath the <%@ Page %> code, add in the following to register the Web User Control:
<%@ Register Src="~/ContactForm.ascx" TagPrefix="WebUserControl" TagName="WebTag" %>
- In the div tags of the body, add in the following:
<!-- Add the web user control in asp -->
<WebUserControl:WebTag id="MyControl" runat="server"/>
<br /><br /
What this has done is registered our user control as WebUserControl:WebTag and then created an instance of it on the Web Form.
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
The second way we can add the Web User Control is in the C# codebehind. To do this, open up the Default.aspx.cs codebehind file for editing and add the following code to the Page_Load event method:
protected void Page_Load(object sender, EventArgs e) |
The ContactForm.ascx looks like this:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="ContactForm" %> |
No comments:
Post a Comment