1

私は次のコードを持っています:

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())

x.Text=member.FirstName;を含む行 FirstNameアイテムをTextBoxID文字列に置き換えたいのですが。TextBoxを自動的にループして入力できるようにします

4

1 に答える 1

2

bindingsourceテキストボックスを作成してからこれにバインドしてみましたか。

実行時に、EFエンティティをバインディングソースにバインドでき、コントロールが自動入力されます。

良い例がここにあります-それはグリッドへのバインドを示していますが、原則は個々のコントロールへのバインドについても同様です。

さらに、単一のエンティティをプルするには、次のことができます。

AccountMember member = context.AccountMember
                              .Single(m => m.AccountMemberId == ModifyUser);

// Bind the fetched entity to the bindingsource and hence to the UI controls
// At runtime this is all that is needed to update the controls, as long as 
// you have set things up at design time.
myBindingSource.DataSource = member;
于 2011-01-13T23:09:17.690 に答える