1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11
12namespace DataBindToCustomObject
13{
14	public class WebForm1 : System.Web.UI.Page
15	{
16		#region Web Form Designer generated code   ...		#endregion
35
36		//Custom object
37		Users mobjUsers = new Users();
38
39		private void Page_Load(object sender, System.EventArgs e)
40		{
41			//Always clear the message and user details on load
42			lblMessage.Text = "";
43			lblUserDetails.Text = "";
44
45			//Are we posting the page back?
46			if (!Page.IsPostBack)
47			{
48				//Load some users into the custom object
49				LoadNewUsers();
50				//Bind the custom object to the datagrid
51				BindDataGrid();
52			}
53			else
54				//Reload the custom object
55				LoadUsersFromSessionState();
56		}
57
58		private void LoadNewUsers()
59		{
60			//Declare my objects
61			UserDetails objUser1, objUser2, objUser3;
62
63			//Load user 1
64			objUser1 = new UserDetails();
65			objUser1.EmailAddress = "brian@brianpautsch.com";
66			objUser1.FirstName = "Brian";
67			objUser1.LastName = "Pautsch";
68			mobjUsers.Add(objUser1);
69
70			//Load user 2
71			objUser2 = new UserDetails();
72			objUser2.EmailAddress = "tracy@brianpautsch.com";
73			objUser2.FirstName = "Tracy";
74			objUser2.LastName = "Pautsch";
75			mobjUsers.Add(objUser2);
76
77			//Load user 3
78			objUser3 = new UserDetails();
79			objUser3.EmailAddress = "ethan@brianpautsch.com";
80			objUser3.FirstName = "Ethan";
81			objUser3.LastName = "Pautsch";
82			mobjUsers.Add(objUser3);
83
84			//Load to session state (for this example)
85			Session["Users"] = mobjUsers;
86		}
87
88		private void LoadUsersFromSessionState()
89		{
90			//Refresh custom object from session state (for this example)
91			mobjUsers = (Users)Session["Users"];
92		}
93
94		private void BindDataGrid()
95		{
96			//Bind the custom object to the datagrid
97			dgUsers.DataSource = mobjUsers;
98			dgUsers.DataBind();
99		}
100
101		private void dgUsers_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
102		{
103			//If this is an item or alternating item
104			if (e.Item.ItemType == ListItemType.Item ||
105				e.Item.ItemType == ListItemType.AlternatingItem)
106			{
107				//Find the Delete linkbutton
108				LinkButton objLink = (LinkButton)e.Item.FindControl("lnkDelete");
109				//Did we find it?
110				if (objLink != null)
111					//Add the javascript "Are you sure..." attribute
112					objLink.Attributes.Add("onclick", "return confirm_delete();");
113			}
114		}
115
116		public void dgUsers_ItemCommand(object sender, DataGridCommandEventArgs e)
117		{
118			//Which action?
119			if (e.CommandName == "View")
120			{
121				try
122				{
123					//Load a user details object by index
124					UserDetails objUser = mobjUsers[e.Item.ItemIndex];
125					//Show the user details in a simple label
126					lblUserDetails.Text = "Email Address: " + objUser.EmailAddress + "<br>" +
127						"First Name: " + objUser.FirstName + "<br>" +
128						"Last Name: " + objUser.LastName + "<br>";
129				}
130				catch (Exception ex)
131				{
132					lblMessage.Text = "Exception: " + ex.Message;;
133				}
134			}
135			else if (e.CommandName == "Delete")
136			{
137				try
138				{
139					//Load a user details object by email address
140					Label objLabel = (Label)e.Item.FindControl("lblEmail");
141					//Did we find it?
142					if (objLabel != null)
143					{
144						UserDetails objUser = mobjUsers[objLabel.Text];
145						//Remove the user
146						mobjUsers.Remove(objUser);
147						//Show message to user
148						lblMessage.Text = "Delete successful!";
149					}
150					else
151						//This should never happen
152						throw new Exception("Email address could not be found.");
153				}
154				catch (Exception ex)
155				{
156					lblMessage.Text = "Exception: " + ex.Message;;
157				}
158			}
159			else
160				return;
161
162			//Rebind the datagrid to reflect changes
163			BindDataGrid();
164
165			//Refresh Session State
166			Session["Users"] = mobjUsers;
167		}
168	}
169}