Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Tuesday, December 4, 2007

Creating a Web User Control

Creating a Custom Web Control.
Within Visual Studio:
To keep organized, I create a company/website/user folder. Within the folder I create another folder named Controls. For example I'll have this directory structure: FooInc/Controls.

Within that folder, rt. click and Add New Item. Select the WebUserControl option. I'll name this one WebUserControl_DatePanel.ascx.

Switch to design view. Add a Panel (named Panel1 by default). Add a Label (named Label1 by default) within Panel1. Set Panel1's background to cyan for a custom feel.

In the load Event of WebUserControl_DatePanel.ascx, add the following:
Label1.Text = DateTime.Now.ToString();

Now save and close those files.

In your project, create a new Web Form named WebUser.aspx. Switch to design view.

From the Solution Explorer, drag the WebUserControl_DatePanel.ascx onto the page (much like adding a CSS file). Switch to Source View.

You will notice the following header:

<%@ Register Src="FooInc/Controls/WebUserControl_DatePanel.ascx" TagName="WebUserControl_DatePanel"
TagPrefix="uc1" %>
You will also notice the following web user control in the body:

<uc1:WebUserControl_DatePanel ID="WebUserControl_DatePanel1" runat="server" />

There it is. You can add custom functions at the web user control source if you want additional functionality.

1 comment:

Anonymous said...

Hello. And Bye.