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