PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

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

 
 
Owen Blacker
Guest
Posts: n/a
 
      19th Jul 2006
I've spent loads of time Googling to try to work this one out and I'm
sure it's something obvious.

I get an InvalidOperationException reading "Databinding methods such as
Eval(), XPath(), and Bind() can only be used in the context of a
databound control" when I start my page. Unless I'm missing something
obvious, this is *not* the same issue as the "2-way databinding
cascading lists" issue in
http://www.webswapp.com/codesamples/...s/default.aspx
(which is linked from everywhere; I kinda wish that were my problem ;o)

I have a templated databound custom server control, that I've built
with
a great deal of help from the two articles by 4GuysFromRolla's Scott
Mitchell (http://masl.to/?S11823B6D and http://masl.to/?P40821B6D, both
on msdn.microsoft.com).

I don't seem to be able to use databinding syntax, however:

<ItemTemplate>
<asp:TableCell runat="server"><%# Eval("First")
%></asp:TableCell>
...
</ItemTemplate>

bound to a List<Pair> gives me an exception:

[InvalidOperationException: Databinding methods such as Eval(),
XPath(),
and Bind() can only be used in the context of a databound control.]
System.Web.UI.Page.GetDataItem() +90
System.Web.UI.TemplateControl.Eval(String expression) +110
ASP.owentest_aspx.__DataBind__control8(Object sender, EventArgs e)
in
c:\Projects\Customisation\RedesignProtoTypeWeb\Owentest.aspx:13
System.Web.UI.Control.OnDataBinding(EventArgs e) +80
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +198
System.Web.UI.Control.DataBind() +12
System.Web.UI.Control.DataBindChildren() +214
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
System.Web.UI.Control.DataBind() +12
System.Web.UI.Control.DataBindChildren() +214
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
System.Web.UI.Control.DataBind() +12
uSwitch.Web.UI.Lego.RepeatingInputRow.CreateItem(Int32 rowIndex,
Table container, Boolean useDataBinding, Object rowData) in

C:\Projects\uSwitch.Web\UI.Lego\InputModule\RepeatingInputRow.cs:361
uSwitch.Web.UI.Lego.RepeatingInputRow.CreateControlHierarchy(Boolean
useDataBinding) in

C:\Projects\uSwitch.Web\UI.Lego\InputModule\RepeatingInputRow.cs:452
uSwitch.Web.UI.Lego.RepeatingInputRow.DataBind() in

C:\Projects\uSwitch.Web\UI.Lego\InputModule\RepeatingInputRow.cs:643
Owentest.Page_Load(Object sender, EventArgs e) in

c:\Projects\Customisation\RedesignProtoTypeWeb\Owentest.aspx.cs:20
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object
o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +3716

I don't get the exception if my databinding control is outside the
table
cell but, for a handful of other reasons, including that my
ItemTemplate
is being instantiated inside a TableRow, that won't work for me.

Any ideas gratefully received, as I'm completely lost.


Owen
--
Owen Blacker, London GB
Say no to ID cards: www.no2id.net
Get your mits off my bits: www.openrightsgroup.org
--
Those who would give up essential liberty to purchase a little
temporary safety, deserve neither liberty nor safety -- Benjamin
Franklin, 1759

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      21st Jul 2006
Owen,

Can you post the entire page code that causes this problem?
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"Owen Blacker" wrote:

> I've spent loads of time Googling to try to work this one out and I'm
> sure it's something obvious.
>
> I get an InvalidOperationException reading "Databinding methods such as
> Eval(), XPath(), and Bind() can only be used in the context of a
> databound control" when I start my page. Unless I'm missing something
> obvious, this is *not* the same issue as the "2-way databinding
> cascading lists" issue in
> http://www.webswapp.com/codesamples/...s/default.aspx
> (which is linked from everywhere; I kinda wish that were my problem ;o)
>
> I have a templated databound custom server control, that I've built
> with
> a great deal of help from the two articles by 4GuysFromRolla's Scott
> Mitchell (http://masl.to/?S11823B6D and http://masl.to/?P40821B6D, both
> on msdn.microsoft.com).
>
> I don't seem to be able to use databinding syntax, however:
>
> <ItemTemplate>
> <asp:TableCell runat="server"><%# Eval("First")
> %></asp:TableCell>
> ...
> </ItemTemplate>
>
> bound to a List<Pair> gives me an exception:
>
> [InvalidOperationException: Databinding methods such as Eval(),
> XPath(),
> and Bind() can only be used in the context of a databound control.]
> System.Web.UI.Page.GetDataItem() +90
> System.Web.UI.TemplateControl.Eval(String expression) +110
> ASP.owentest_aspx.__DataBind__control8(Object sender, EventArgs e)
> in
> c:\Projects\Customisation\RedesignProtoTypeWeb\Owentest.aspx:13
> System.Web.UI.Control.OnDataBinding(EventArgs e) +80
> System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +198
> System.Web.UI.Control.DataBind() +12
> System.Web.UI.Control.DataBindChildren() +214
> System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
> System.Web.UI.Control.DataBind() +12
> System.Web.UI.Control.DataBindChildren() +214
> System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
> System.Web.UI.Control.DataBind() +12
> uSwitch.Web.UI.Lego.RepeatingInputRow.CreateItem(Int32 rowIndex,
> Table container, Boolean useDataBinding, Object rowData) in
>
> C:\Projects\uSwitch.Web\UI.Lego\InputModule\RepeatingInputRow.cs:361
> uSwitch.Web.UI.Lego.RepeatingInputRow.CreateControlHierarchy(Boolean
> useDataBinding) in
>
> C:\Projects\uSwitch.Web\UI.Lego\InputModule\RepeatingInputRow.cs:452
> uSwitch.Web.UI.Lego.RepeatingInputRow.DataBind() in
>
> C:\Projects\uSwitch.Web\UI.Lego\InputModule\RepeatingInputRow.cs:643
> Owentest.Page_Load(Object sender, EventArgs e) in
>
> c:\Projects\Customisation\RedesignProtoTypeWeb\Owentest.aspx.cs:20
> System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object
> o, Object t, EventArgs e) +13
> System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
> sender, EventArgs e) +45
> System.Web.UI.Control.OnLoad(EventArgs e) +80
> System.Web.UI.Control.LoadRecursive() +49
> System.Web.UI.Page.ProcessRequestMain(Boolean
> includeStagesBeforeAsyncPoint, Boolean
> includeStagesAfterAsyncPoint) +3716
>
> I don't get the exception if my databinding control is outside the
> table
> cell but, for a handful of other reasons, including that my
> ItemTemplate
> is being instantiated inside a TableRow, that won't work for me.
>
> Any ideas gratefully received, as I'm completely lost.
>
>
> Owen
> --
> Owen Blacker, London GB
> Say no to ID cards: www.no2id.net
> Get your mits off my bits: www.openrightsgroup.org
> --
> Those who would give up essential liberty to purchase a little
> temporary safety, deserve neither liberty nor safety -- Benjamin
> Franklin, 1759
>
>

 
Reply With Quote
 
New Member
Join Date: Sep 2011
Posts: 1
 
      26th Sep 2011
Hi, I got the same problem and, spent about three days trying to solve it. I found that Container.DataItem works perfectly. Please if you found the solution for this problem please post it.

Here is the full code:
public class Grid : CompositeDataBoundControl, INamingContainer
{
private List<Column> columns = new List<Column>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(ColumnCollectionEditor), typeof(UITypeEditor))]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public List<Column> Columns
{ ...}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<table border=\"1\">");
this.RenderChildren(writer);
writer.Write("</table>");
}
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding){
this.Controls.Clear();
int index = 0;
foreach (object dataItem in dataSource){
GridRow row = new GridRow(dataItem, index++, this.columns);
this.Controls.Add(row);
}
return index;
}
public override void DataBind(){
base.OnDataBinding(EventArgs.Empty);
base.DataBind();
this.DataBindChildren();
}
}

public class GridRow : CompositeControl, IDataItemContainer, INamingContainer{
public object DataItem { get; private set; }
public int DataItemIndex { get; private set; }
public int DisplayIndex { get; private set; }
private List<Column> columns;

internal GridRow(object dataItem, int index, List<Column> columns){
this.DataItem = dataItem;
this.DataItemIndex = index;
this.DisplayIndex = index;
this.columns = columns;
}
protected override void Render(HtmlTextWriter writer){
writer.Write("<tr>");
this.RenderChildren(writer);
writer.Write("</tr>");
}
protected override void CreateChildControls(){
this.Controls.Clear();
foreach (Column column in columns){
GridCell cell = new GridCell(this.DataItem, this.DataItemIndex);
this.Controls.Add(cell);
column.CellTemplate.InstantiateIn(cell);
}
}
public override void DataBind(){
base.OnDataBinding(EventArgs.Empty);
this.CreateChildControls();
this.ChildControlsCreated = true;
base.DataBind();
this.DataBindChildren();
}
}

public class GridCell : Control{
public object DataItem { get; private set; }
public int DataItemIndex { get; private set; }
public int DisplayIndex { get; private set; }

internal GridCell(object dataItem, int index)
{
this.DataItem = dataItem;
this.DataItemIndex = index;
this.DisplayIndex = index;
}

protected override void Render(HtmlTextWriter writer)
{
writer.Write("<td>");
this.RenderChildren(writer);
writer.Write("</td>");
}
}

public class Column{
public String Name { get; set; }
public String ID { get; set; }
public string Title { get; set; }

[TemplateContainer(typeof(GridRow))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate CellTemplate { get; set; }
}
<rcn:Grid ID="grd" OnGridRowDataBound="grd_GridRowDataBound" runat="server">
<rcn:Column ID="Column2" Name="Name2" Title="Name">
<CellTemplate>
<%# Eval("Name") %>
</CellTemplate>
</rcn:Column>
</rcn:Grid>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop down list error: "Databinding methods such as Eval(), XPath(),and Bind() can only be used in the context of a databound control." yosri2005@gmail.com Microsoft ASP .NET 0 13th Dec 2007 02:00 PM
Does databinding affect the source data used to bind to the control? mark4asp Microsoft ASP .NET 1 6th Jul 2007 04:58 PM
Eval a field in a Xpath Filtered Data Source gouraud@gmail.com Microsoft ASP .NET 0 16th Aug 2006 01:26 PM
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control" moi Microsoft ASP .NET 0 9th Mar 2006 11:38 AM
Formatting Databound Control Disabling CurrencyManager.methods Johnny Liner Microsoft VB .NET 1 1st Feb 2005 02:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 PM.