Abstract
A simple .net class that will prepopulate form data on a page. My requirements were to be able to find a control anywhere on a page (with or without a masterpage) and still be able to control the look of the form. I did not want to dynamically create the controls because I want to easily control the look of the form without adding any metadata in to the database.
Meat and Potatoes
Inevitably when building a website I will have a page (or multiple) that should display information about an item, account, person, etc. For example, I always have a "My Account" page where I will show the user data we have collected that could be everything from their name and email address to their shoe or ring size. There could be a dozen or more fields displayed. Some fields may be read-only (Label) and some may be editable (TextBox). I start out building the page in the designer with a table and then adding all of my fields as either a TextBox or Label. Then, I switch to the code behind and poulate a DataTable with the information I need. The DataTable is always a single row with all that person's data. I then have to type all the fields names and set their Text value to the DataTable's records. Rinse and repeat, ice the wrist, rinse and repeat, etc.
In the .aspx page I may have:
[code:c#]
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td align="left">First Name</td>
<td align="left"><asp:Label ID="aFName" runat="server" /></td>
</tr>
<tr>
<td align="left">Last Name</td>
<td align="left"><asp:Label ID="aLName" runat="server" /></td>
</tr>
.
.
.
</table>
[/code]
In the code behind I would then have to do this:
[code:c#]
aFName.Text = dt.Rows[0]["aFName"].ToString();
aLName.Text = dt.Rows[0]["aLName"].ToString();
aEmail.Text = dt.Rows[0]["aEmail"].ToString();
aDisplayName.Text = dt.Rows[0]["aDisplayName"].ToString();
aCellPhone.Text = dt.Rows[0]["aCellPhone"].ToString();
aSignUpDate.Text = dt.Rows[0]["aSignUpDate"].ToString();
atDesc.Text = dt.Rows[0]["atDesc"].ToString();
.
.
.
[/code]
This is an inefficient use of time and caffeine. Since I do this all the time, I decided to write a class that does this in a more efficient way. When you create the page design you have to name the controls the exact same name (case sensitive) as the column in your DataTable. We loop through the DataTable and do a FindControl to then populate the data. This worked fine until I created a page with a MasterPage - ugh! FindControl isn't recursive so I hunted online and found a recursive FindControl and included it in my class. Now it works on all pages. I have cut my time (not caffeine intake) down considerably. Now I can use that found time to blog :)
Here's the class (webhelper.cs):
[code:c#]
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Form functions
/// </summary>
public class webhelper
{
/// <summary>
/// Finds a control recursively. Works in a page that has a masterpage, otherwise FindControl will return a null.
/// </summary>
/// <param name="root">Parent to search for - use "this"</param>
/// <param name="id">Name of control you are searching for</param>
/// <returns>Control - control you are searching for or a null if it's not found</returns>
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
/// <summary>
/// Will pre-populate textbox and labels on a page with information from the first row of the DataTable.
/// Common use would be an account information page where you only have one row returned.
/// TextBox and Label ID's must match the column name in the DataTable.
/// </summary>
/// <param name="root">Send "this" as the page root.</param>
/// <param name="dt">DataTable with a single row. If there are multiple rows it will only prepopulate the form with the first row.</param>
public static void populateForm(Control root, DataTable dt)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
Control c = FindControlRecursive(root, dt.Columns[i].ToString());
if (c != null)
{
if (c.GetType() == typeof(TextBox))
{
TextBox myBox = (TextBox)c;
myBox.Text = dt.Rows[0][i].ToString();
}
else if (c.GetType() == typeof(Label))
{
Label myLabel = (Label)c;
myLabel.Text = dt.Rows[0][i].ToString();
}
}
}
}
}
[/code]
And here is how you use it on a page (dt has been defined as a DataTable and is populated with account information):
[code:c#]
webhelper.populateForm(this, dt);
[/code]
Wow, what an improvement in typing, huh? In a future blog I'll show you how to do the same thing for saving the page.
You can download the class here.
Currently rated 1.0 by 1 people
- Currently 1/5 Stars.
- 1
- 2
- 3
- 4
- 5