Click here to Skip to main content
15,891,763 members
Articles / Web Development / ASP.NET
Article

To derive your personal WebControl of .NET FrameWork

Rate me:
Please Sign up or sign in to vote.
1.00/5 (3 votes)
28 Oct 2002 69.6K   322   14   8
Its personalized control, folloies yours necessities and standardizes your font , color nd others

Sample Image - toderiveyourcontrol.jpg

Benefits

1 - Standard of software factory

2 - Visual inheritance (your controls childs inherits the bahivor and visual design

3 - Standard of code

4 - Over reutilization

Conclusion

Highly indicated for it manufactures of software

Step 1

Create WebControlLibrary and add source code

To personalize all the functionalities of its grid, for example

C#
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data; 
using System.Collections; 
using System.IO;
using System.Drawing;  

namespace Personal.Web.UI.WebControls
{
	//To derive of .NET Framework
	public class DataGrid :System.Web.UI.WebControls.DataGrid 
	{
		public DataGrid()
		{
		}

		protected override void Render(HtmlTextWriter output)
		{ 	
			//personalized properties 
			this.AutoGenerateColumns = false;
			//personalized Border
			this.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;      
			//personalized GridLines
			this.GridLines = GridLines.Both; 
			
			//personalized SelectedItemStyle
			this.SelectedItemStyle.Font.Name = "Tahoma";
			this.SelectedItemStyle.Font.Size = 8;
			this.SelectedItemStyle.Font.Bold = true;			
			this.SelectedItemStyle.ForeColor = System.Drawing.Color.White;
			this.SelectedItemStyle.BackColor = System.Drawing.Color.FromName("#000099");			
			
			//personalize AlternatingItemStyle
			this.AlternatingItemStyle.Font.Name = "Tahoma";
			this.AlternatingItemStyle.Font.Size = 8;			
			this.AlternatingItemStyle.ForeColor = System.Drawing.Color.Black;
			this.AlternatingItemStyle.BackColor = System.Drawing.Color.WhiteSmoke;
			
			//personalize ItemStyle
			this.ItemStyle.Font.Name = "Tahoma";
			this.ItemStyle.Font.Size = 8;			
			this.ItemStyle.ForeColor = System.Drawing.Color.Black;
			this.ItemStyle.BackColor = System.Drawing.Color.White;

			//personalize HeaderStyle
			this.HeaderStyle.Font.Name = "Tahoma";
			this.HeaderStyle.Font.Size = 8;
			this.HeaderStyle.Font.Bold = true;
			//this.HeaderStyle.ForeColor = System.Drawing.Color.DarkGray;
			this.HeaderStyle.BackColor = System.Drawing.Color.DarkGray;

			//personalize PagerStyle
			this.PagerStyle.Font.Name = "Tahoma";
			this.PagerStyle.Font.Size = 8;
			this.PagerStyle.BackColor = System.Drawing.Color.DarkGray;

			//personalize Paging
			this.AllowPaging = true;
			this.PagerStyle.Mode = PagerMode.NumericPages;    

			this.PagerStyle.HorizontalAlign = HorizontalAlign.Center; 
  
			base.Render(output);  			
		}	

		//it implements yours functionalities
		#region Public Aux Objects 

		TextBox txt = null;
		DropDownList ddl = null;
		CheckBox chk = null;
		Image img = null;
		int record ; 

		#endregion

		#region Methods

		public void DataBindObjects(DataGrid dg,DataSet ds,int pageindex,int pagesize)
		{
			int i = 0;
			int ids = 0;	

			//Verify the not null properties
			//Exeption Controls
			if (this.editable == null ||
				this.objectname == null ||
				this.fieldname == null ||
				this.editable.Count != this.fieldname.Count ||
				this.editable.Count != this.objectname.Count ||
				this.fieldname.Count != this.editable.Count	||
				this.fieldname.Count != this.objectname.Count || 
				this.objectname.Count != this.fieldname.Count ||
				this.objectname.Count != this.editable.Count )
			{
				string msg = "n";
				msg += "DropDown,TextBox e CheckBox :\n";
				msg += "c.FieldName = 'Field'\n"; 
				msg += "c.ObjectName = 'TextBox3'\n";
				msg += "c.Editable = true\n ";
				msg += "  'c'  ";

				throw new Exception(msg); 
			}

			//Move the itens of the grid
			for (ids=0;ids<dg.Items.Count;ids++)
			{
				//to select the item
				DataGridItem dgi = dg.Items[ids]; 
				
				//to verify paging
				record = (pagesize * pageindex) + ids;

				//to verify fieldname collections
				for (i=0;i<this.fieldname.Count;i++)
				{				

					//find the control
					object obj = dgi.FindControl(this.objectname[i].ToString());   

					//to verify type
					//this editable all itens
					if (obj is TextBox)
					{
						txt = (TextBox) dgi.FindControl(this.objectname[i].ToString());   						
						if (this.editable != null)
						{							
							if ((bool)this.editable[i] == true)
							{
								txt.ReadOnly = false;
							}
							else
							{
								txt.ReadOnly = true;
							}
						}
						else
						{
							txt.Enabled = true;
						}
					}
					else if (obj is Image)
					{
						img = (Image) dgi.FindControl(this.objectname[i].ToString());   						
					}
					else if (obj is DropDownList)
					{
						ddl = (DropDownList) dgi.FindControl(this.objectname[i].ToString());   						
						if (this.editable != null)
						{
							if ((bool)this.editable[i] == true)
							{
								ddl.Enabled = true;
							}
							else
							{
								ddl.Enabled = false;
							}
						}
						else
						{
							ddl.Enabled = true;
						}

					}
					else if (obj is CheckBox)
					{
						chk = (CheckBox) dgi.FindControl(this.objectname[i].ToString());   						
						if (this.editable != null)
						{
							if ((bool)this.editable[i] == true)
							{
								chk.Enabled = true;
							}
							else
							{
								chk.Enabled = false;
							}
						}
						else
						{
							chk.Enabled = true;
						}

					}				
				}
				
				i=0;

				//bind the dato to fieldname 
				//correspondent to collection fieldname 
				for (i=0;i<this.fieldname.Count;i++)
				{
					//Check the objects in ther columns 
					//it is applied to all itens
					if (txt !=null && this.objectname[i].ToString().ToUpper() == txt.ID.ToUpper())
					{						
						txt.Text = ds.Tables[0].Rows[record]
							[this.fieldname[i].ToString()].ToString();  
					}
					else if (img !=null && this.objectname[i].ToString().ToUpper()
						== img.ID.ToUpper())
					{
						try
						{
							//convert to binary data 
							//your database field type image
							byte[] bits = (byte[]) ds.Tables[0].Rows[record]
								[this.fieldname[i].ToString()];  
							MemoryStream memorybits = new MemoryStream(bits);							
							System.Drawing.Image bitmap = System.Drawing.Image.FromStream(memorybits);    
							string imageharddisk = @"c:\temp\img\" + (img.GetHashCode() + i); 
							bitmap.Save(imageharddisk);  
							img.ImageUrl = imageharddisk;
						}
						catch
						{
						}
					}																	 

					else if (ddl !=null && this.objectname[i].ToString().ToUpper()
						== ddl.ID.ToUpper())
					{						
						if(this.DataFieldText == null |
							this.DataFieldValue == null)
						{
							throw new NullReferenceException("DataFieldText ou DataFieldValue, não pode ser nulo");
						}

						ddl.DataTextField = this.DataFieldText;
						ddl.DataValueField = this.DataFieldValue;

						if (this.dropdowndata == null)
						{
							throw new NullReferenceException("DropDownData, não pode ser nulo");
						}

						ddl.DataSource = (DataSet) this.dropdowndata[i]; 
						ddl.DataBind();
 
						this.PositionObjectList(
							ddl,
							ds.Tables[0].Rows[record][this.fieldname[i].ToString()].ToString());

					}
					else if (chk !=null && this.objectname[i].ToString().ToUpper()
						== chk.ID.ToUpper())
					{
						if (ds.Tables[0].Rows[record][this.fieldname[i].ToString()].ToString().Trim() == "0")
						{
							chk.Checked = false;
						}
						else
						{
							chk.Checked = true;
						}

					}

				}
			}			
		}

		//position  object DropDownList
		public void PositionObjectList(DropDownList objectlist ,
			string val)
						
		{			
			int i;

			for (i = 0;i < (objectlist.Items.Count);i++)
			{
				objectlist.SelectedIndex = i;
				if (objectlist.SelectedItem.Value == val)
				{
					objectlist.SelectedItem.Selected = true;
					break;
				}

			}
		}
	
		//overload 
		public void PositionObjectList(ListBox objectlist ,
			string val)
						
		{ 		
			int i;

			for (i = 0;i < (objectlist.Items.Count);i++)
			{
				objectlist.SelectedIndex = i;
				if (objectlist.SelectedItem.Value == val)
				{
					objectlist.SelectedItem.Selected = true;
					break;
				}

			}
		}

		#endregion

		#region Properties
		private ArrayList fieldname;
		private ArrayList objectname;
		private ArrayList editable;
		private ArrayList dropdowndata;
		private string datafieldtext;
		private string datafieldvalue;

		public string DataFieldText
		{
			set
			{
				datafieldtext = value;
			}
			get
			{
				return datafieldtext;
			}
		}

		public string DataFieldValue
		{
			set
			{
				datafieldvalue = value;
			}
			get
			{
				return datafieldvalue ;
			}
		}

		public DataSet DropDownData
		{
			set
			{
				if (dropdowndata  == null)
				{
					dropdowndata  = new ArrayList();
				}
				dropdowndata .Add (value);				
			}
		}

		public string FieldName
		{
			set
			{
				if (fieldname == null)
				{
					fieldname = new ArrayList();
				}
				fieldname.Add (value);
			}
		}

		public string ObjectName
		{
			set
			{
				if (objectname == null)
				{
					objectname = new ArrayList(); 
				}
				objectname.Add(value);
			}
		}

		public bool Editable
		{
			set
			{
				if (editable == null)
				{
					editable = new ArrayList(); 
				}
				editable.Add(value);
			}
		}

		#endregion
	}
}

The Key is Standard for your Software Factory

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionset forecolor to a particular row in a datagrid using C#.Net Pin
S.Srinivasaga Perumal28-Sep-05 23:52
S.Srinivasaga Perumal28-Sep-05 23:52 
GeneralExample code Pin
kloppie6-Apr-05 20:09
kloppie6-Apr-05 20:09 
GeneralSilly Pin
Mike Chaliy20-Mar-05 10:23
Mike Chaliy20-Mar-05 10:23 
GeneralRe: Silly Pin
Esteves21-Mar-05 11:26
Esteves21-Mar-05 11:26 
QuestionHow to use it in a page Pin
kmmfoo14-Feb-04 4:52
kmmfoo14-Feb-04 4:52 
AnswerRe: How to use it in a page Pin
Esteves14-Feb-04 12:08
Esteves14-Feb-04 12:08 
Generalinteresting, but i can't figure out what this does ... Pin
Bill Woodruff29-Oct-02 3:54
Bill Woodruff29-Oct-02 3:54 
GeneralRe: interesting, but i can't figure out what this does ... Pin
Esteves29-Oct-02 9:31
Esteves29-Oct-02 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.