65.9K
CodeProject is changing. Read more.
Home

To derive your personal WebControl of .NET FrameWork

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7 votes)

Oct 21, 2002

viewsIcon

70071

downloadIcon

322

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

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