Click here to Skip to main content
Licence 
First Posted 20 Oct 2002
Views 58,945
Bookmarked 14 times

To derive your personal WebControl of .NET FrameWork

By | 28 Oct 2002 | Article
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

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

About the Author

Esteves

Web Developer

Brazil Brazil

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionset forecolor to a particular row in a datagrid using C#.Net PinmemberS.Srinivasaga Perumal23:52 28 Sep '05  
GeneralExample code Pinmemberkloppie20:09 6 Apr '05  
GeneralSilly PinmemberMike Chaliy10:23 20 Mar '05  
GeneralRe: Silly PinmemberEsteves11:26 21 Mar '05  
QuestionHow to use it in a page Pinmemberkmmfoo4:52 14 Feb '04  
AnswerRe: How to use it in a page PinmemberEsteves12:08 14 Feb '04  
Generalinteresting, but i can't figure out what this does ... PinsussBill Woodruff3:54 29 Oct '02  
GeneralRe: interesting, but i can't figure out what this does ... PinmemberEsteves9:31 29 Oct '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 29 Oct 2002
Article Copyright 2002 by Esteves
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid