I have the following code:
IList<AccountMember> query;
using (DBEntities context = new DBEntities())
{
Guid ModifyUser = new Guid(Session["ModifyUser"].ToString());
query = (from AccountMember member in context.AccountMember
where member.AccountMemberId == ModifyUser
select member).ToList();
foreach (AccountMember member in query)
{
//this.FirstName.Text = member.FirstName;
ControlCollection controls = this.Controls;
foreach (Control control in controls)
{
if (control is TextBox)
{
TextBox x = (TextBox)control;
x.Text = member.FirstName; // want to replace the .FirstName with the TextBox ID value somehow
}
} // foreach (Control control in controls)
} // foreach (AccountMember member in query)
} // using (DBEntities context = new DBEntities())
At the line containing x.Text = member.FirstName; I would like to replace the FirstName item with the TextBox ID string. So that I can just loop and populate my TextBoxes automatically
Have you tried creating a
bindingsourcethen binding your text boxes to this.At runtime you can then bind your EF entity to the binding source and your controls will be autopopulated.
A nice example can be found here – it shows binding to a grid, but the principle is similar for binding to individual controls.
Additionally to pull a single entity you can do: