Page 39 - DCAP312_WEB_TECHNOLOGIES_II
P. 39
Unit 2: Introduction to C#
2.2 Web Control Notes
Web User organize or the .ascx box file in asp .net is a substitution for the comprise file
characteristic in ASP. It actually is a positive development from the asp model. Classic asp had
used .inc file and included it wherever we needed it. Though this model is also similar, Web
User Controls have a lot of other features added along with them.
Web user controls are imitative from System.Web.UI.UserControl namespace. These controls once
created, can be additional to the aspx page either at propose point or programmatically during
run time. But they lack the design time support of setting the properties created along with the
control. They cannot run on their own and they need to stand on another platform like an aspx
page. Even if we try to load it on a web browser, IIS will not serve the files of the type .ascx.
2.2.1 Adding a Web User Control
• Create a new ASP .NET web application using Visual Studio .NET.
• Add a Web User Control to the project. Name it as SampleUserControl.ascx.
• Add a Panel Control on the user control form and name it as pnlSample.
• This Panel control is going to be used to hold our other controls.
• Add a TextBox as txtSearch and a Command button as cmdSearch. These will be used for
entering a site search term and doing a search.
• Add a property to the User Control as follows.
protected Color backColor;
public Color BackColor
{
get
{
return backColor;
}
set
{
backColor = value;
}
}
• The above can be used set the Background color of the control. Add the following code
to the Page_Load event of the control.
• private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
pnlSample.BackColor = backColor;
}
• Now add another web form as TestPage.aspx.
• Drag and drop the SampleUserControl.ascx into the Web form. This will put the following
entries in the aspx page.
LOVELY PROFESSIONAL UNIVERSITY 33