Tuesday, February 24, 2009

Usage of User Control for ASP.NET

Today I try something of creating an "User Control" for an ASP.NET based web site, and pass parameter to the control. Below is a summary

1. For an ASP.NET project, select to add “user control” as shown below


2. For the ASCX file, add resource to UI of the control as below


3. For the .CS file related to the user control add proper logic as shown below


4. Drag and drop the “User Control” to a web form which will use the control. Related code as below will be generated automatically
@ Register Src="ctrlCalendar.ascx" TagName="ctrlCalendar" TagPrefix="uc1"

<%@ Register Src="ctrlCalendar.ascx" TagName="ctrlCalendar" TagPrefix="uc1" %>5. Do the following if wants to pass parameter from a web form to the control:
..Add variable such as “public string ButName;” in the control
..Add command as below in the Page_Load of the web form:
CtrlCalendar1.ButName = "From";
CtrlCalendar2.ButName = "To";
..Add command as below in the Page_Load of the user control:
this.Button_1.Value = ButName;

No comments: