UserControl and DataBinding

K

Kent Boogaart

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top