.Net Framework, Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML

Ado.netAsp.Net 2.0SQLC-SharpXMLFrameworkIISXMLWebServiceArchitecture
UMLProject ManagementSDLCMethodologiesDesign PatterenOOPWCF.Net RemotingWWF
MVPMVCSilver LightN-HibernateAjaxJ-QuerySEO-MarketingSite MapPhp

Customcontrol in Asp.net

Designing a customcontrol as simple as designing a user control in asp.net, and more fun, once you create a custom control you can use in any other project which is not posssible in user control.

Let's start with baby step
Add a class file with name "MyCustomControl.cs" & add this namespace
using System.ComponentModel;

Now inherit the class from System.Web.UI.WebControls.WebControl,INamingContainer
Design customcontrol in asp.net
using System;
1:using System.Web.UI;
2:using System.Web.UI.WebControls;
3:using System.ComponentModel;
4:using System.Text;
5:using System.IO;
6:
7:namespace Arindam
8:{
9:
10: [DefaultProperty("Text"),
11: ToolboxData("<{0}:CodeContainer runat=server></{0}:CodeContainer>")]
12: public class MyCustomControl: System.Web.UI.WebControls.WebControl,INamingContainer
13: {
14:
15: string _clientScriptPath = "";
16:
17: override protected void OnInit(EventArgs e)
18: {
19: this._clientScriptPath = "Set any property value as per your need";
20: Intialize();
21: }
22:
23:
24: private void Intialize()
25: {
26:
27: this.Load += new EventHandler(CodeContainer_Load);
28: }
29:
30:
31: protected void CodeContainer_Load(object source, EventArgs e)
32: {
33: // here you write code what your control to do while loading.
34: }
35: }
36:}
37:


You can add any number of child controls in in your costom control, you also can set the rendering order as per your need.
override CreateChildControls() in customcontrol in asp.net
using System;
1:using System.Web.UI;
2:using System.Web.UI.WebControls;
3:using System.ComponentModel;
4:using System.Text;
5:using System.IO;
6:
7:namespace Arindam
8:{
9:
10: [DefaultProperty("Text"),
11: ToolboxData("<{0}:CodeContainer runat=server></{0}:CodeContainer>")]
12: public class MyCustomControl: System.Web.UI.WebControls.WebControl,INamingContainer
13: {
14: string _clientScriptPath = "";
15: Panel objPanel;
16: Literal objLtrl;
17: Label objLab;
18:
19: protected override void CreateChildControls()
20: {
21:
22: objPanel=new Panel();
23: objLtrl=new Literal ();
24: objLab=new Label();
25:
26: objPanel.ID ="CodeZone";
27:
28: objLab.Text = this.Title;
29:
30: objLab.Font.Bold = true;
31: objLab.BorderWidth = Unit.Pixel(1);
32: objLab.BorderStyle = BorderStyle.Dotted;
33: objLab.BorderColor = System.Drawing.Color.FromName("#A0A0A1");
34: // int _imgwidth = objImg == null ? 0 : (int)objImg.Width;
35: objLab.Width = Unit.Pixel(this.AreaWidth - 14);
36: objLtrl.Text = this.Text ;
37:
38: objPanel.BackColor = System.Drawing.Color.Cornsilk ;
39: objPanel.Height = Unit.Pixel(this.AreaHeight) ;
40: objPanel.Width = Unit.Pixel(this.AreaWidth) ;
41: objPanel.BorderWidth = Unit.Pixel(1);
42: objPanel.Controls.Add(objLtrl);
43:
44:// set rendering order here .
45: Controls.Add(objImg);
46: Controls.Add(objLab);
47: Controls.Add(objPanel);
48:
49:
50:
51: base.CreateChildControls ();
52: }
53: }
54:}
55:


Now see how to create properties in customcontrol
override CreateChildControls() in customcontrol in asp.net
using System;
1:using System.Web.UI;
2:using System.Web.UI.WebControls;
3:using System.ComponentModel;
4:using System.Text;
5:using System.IO;
6:
7:namespace Arindam
8:{
9:
10: [DefaultProperty("Text"),
11: ToolboxData("<{0}:CodeContainer runat=server></{0}:CodeContainer>")]
12: public class MyCustomControl: System.Web.UI.WebControls.WebControl,INamingContainer
13: {
14: string _filePath;
15:
16:
17: /// <summary>
18: /// sample : RootFolder\SubFolder\file.cs,.sql,.xml etc
19: /// </summary>
20: [Bindable(true),
21: Category("Appearance"),
22: DefaultValue("")]
23: public string FilePath
24: {
25: get
26: {
27: return _filePath;
28: }
29:
30: set
31: {
32: _filePath = value;
33: }
34: }
35:
36: }
37:}
38:
Leave your comment.
Name
Email
Comments
Tell Us4519+5 =

Reduce your Dialy IT Cost, Consult with us
Stay healthy in recession

Advertisement
Sponsored by

Privacy Policy ©2009 ETG Consultancy, All Rights Reserved Terms & Conditions
Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML