IT Community - Software Programming, Web Development and Technical Support

How to create User Control in asp.net?

This is a discussion on How to create User Control in asp.net? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi, Can any one suggest how to create user-control in asp.net......


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-23-2008, 11:56 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile How to create User Control in asp.net?

Hi,
Can any one suggest how to create user-control in asp.net...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-23-2008, 08:15 PM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: How to create User Control in asp.net?

In their simplest form, ASP.NET user controls are what developers always wanted from server-side includes. With include files, you could clean up your code and put useful functionality into its own file in order to 1) find it more easily and 2) use it more often and more safely than cutting and pasting the code from application to application. If you fixed or extended the functionality, it was in one place and generally was reflected across all of the applications in which it was being used (at least in some cases).

The one, big, and very limiting problem was that, if you included the same file more than once in the same ASP file, you'd receive the dreaded Named redefined error once your ASP page was rendered in the browser. Well, Microsoft has finally answered your long, unanswered prayers with ASP.NET server controls. The user control is the most basic of this group and the easiest to develop, implement and, more importantly, understand. Ultimately, a custom control or server control will give you the most flexibility and extensibility (meaning one instance will work across web applications whereas the user control must be included in each application that uses it). However, I believe it is the best place to start in order to understand how server controls work and how much power they put at your development fingertips.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-23-2008, 08:20 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: How to create User Control in asp.net?

Hai,
Could u paste the code for creating usercontrols..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-24-2008, 09:22 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: How to create User Control in asp.net?

Hi poornima,
Check the following link to create user control
How to: Create an ASP.NET User Control
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-15-2008, 07:55 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: How to create User Control in asp.net?

hi,
Thanks shalini.
Could u refer some more websites for creating userControl?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-16-2008, 12:50 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Re: How to create User Control in asp.net?

Hi,

Web User control or the .ascx file in asp .net is a replacement for the include file feature in ASP. It actually is a positive evolution 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 derived from System.Web.UI.UserControl namespace. These controls once created, can be added to the aspx page either at design time 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.

Creating a Web User Control:

1. Create a new ASP .Net web application using Visual Studio .Net.
2. Add a Web User Control to the project. Name it as SampleUserControl.ascx.
3. Add a Panel Control on the user control form and name it as pnlSample.
4. This Panel control is going to be used to hold our other controls.
5. 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.
6. Add a property to the User Control as follows.

Quote:
protected Color backColor;

public Color BackColor
{

get
{
return backColor;
}
set
{
backColor = value;
}

}
7. The above can be used set the Background color of the control. Add the following code to the Page_Load event of the control.
8.
Quote:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
pnlSample.BackColor = backColor;
}
9. Now Add another web form as TestPage.aspx.
10. Drag and drop the SampleUserControl.ascx into the Web form. This will put the following entries in the aspx page.
11.
Quote:
<%@ Register TagPrefix="uc1" TagName="SampleUserControl" Src="SampleUserControl.ascx" %>.
This code registers the control with the current aspx page.
12.
Quote:
<uc1:SampleUserControl id="SampleUserControl1" runat="server"></uc1:SampleUserControl>.
This entry will be found inside the <form> .. </form> tag.
13. Build the application.
14. Select and view the TestPage.aspx in the browser. This will show the aspx page with the control loaded with it.

Now if we need we can change the background color to Gray, by adding the property in the control as follows.

Quote:
<uc1:SampleUserControl BackColor="Gray" id="SampleUserControl1" runat="server"></uc1:SampleUserControl>
Now if the application is built, the testpage.aspx will show our user control with a gray background.
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-16-2008, 12:51 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Re: How to create User Control in asp.net?

Advantages of a Web User Control:

The biggest advantage of the Web User controls is that they can be created as a site template and used through out the site. For example they can be made to contain the Menu/Link structure of a site and can be used at all the other aspx pages.

This means the following :

1. If the website introduces a new site-wide link within the current layout/structure, it is enough if we put it on the user control once. All pages will be updated once if the web user control is used in them.
2. If there is any link to be corrected, it can be done once at the server side.
3. The .ascx files can either be used as a simple alternative to the plain HTML or they can also be used to respond to events: This means even custom code can be created against them and put in the code behind files.

Drawbacks / Disadvantages:

Though the User controls offer a flexibility of having a site wide modifications, if the whole structure of the site changes, the HTML/aspx code of all the pages should be modified. But as long as the site maintains a same layout, then Web User controls are the number one choice for maintaining the generic layout of the site.

Another disadvantage is It can not be just be simply referenced for using in a different project. If we want to use this User Control in a different project, we have to copy the file and modify the namespace to the host namespace name.
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-18-2008, 08:01 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: How to create User Control in asp.net?

Hi,
Thanks for all ur replies.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to control the webcam using C#? kingmaker C# Programming 3 09-25-2008 08:01 PM
publicdomainregistry - control panel? annerz General Web hosting Discussions 0 11-06-2007 08:10 PM
How do I get the value of a form control? itbarota HTML, CSS and Javascript Coding Techniques 2 10-26-2007 03:13 AM
What is the most user-friendly, easiest software to use to create my own Web site? preet HTML, CSS and Javascript Coding Techniques 0 10-04-2007 01:38 AM
use of Custom Control and User Control? a.deeban ASP and ASP.NET Programming 1 08-20-2007 07:25 AM


All times are GMT -7. The time now is 02:12 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0