UserControl and DataBinding

Discussion in 'ASP .Net Building Controls' started by Kent Boogaart, Mar 5, 2006.

  1. Hi,

    I am trying to achieve something that I thought would be fairly simple, but
    am having trouble with DataBinding and UserControls. My user control has an
    interface like this:

    public class MyUserControl : UserControl
    {
    public string ProductId
    {
    get { return ViewState["productId"] as string;}
    set { ViewState["productId"] = value; }
    }
    }

    I am using it in a page that binds to the ProductId property from within a
    GridView:

    <asp:GridView ...>
    <app:MyUserControl ProductId='<%# Eval("Id") %>'/>
    </asp:GridView>

    My user control needs to load the product from the database on each request
    (it may cache it later but I'm just loading every time for now). I don't
    know where to put the code to do this. It needs to happen *after* data
    binding so I know the product ID. I tried overriding OnLoad but data binding
    hadn't occurred yet. If I explicitly call DataBind() from OnLoad() I get:

    Databinding methods such as Eval(), XPath(), and Bind() can only be used in
    the context of a databound control.

    I'm really confused - any help greatly appreciated.

    Thanks,
    Kent
     
    Kent Boogaart, Mar 5, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Wright via .NET 247
    Replies:
    2
    Views:
    730
    Brian W
    Jun 12, 2004
  2. =?Utf-8?B?dmJNZW50YWw=?=
    Replies:
    0
    Views:
    355
    =?Utf-8?B?dmJNZW50YWw=?=
    Nov 1, 2004
  3. Leo
    Replies:
    0
    Views:
    398
  4. jobs
    Replies:
    0
    Views:
    1,776
  5. Earl Teigrob
    Replies:
    1
    Views:
    447
    Shravan
    Mar 1, 2004
Loading...

Share This Page