DataBinder.Eval error in asp.net

1.5k views Asked by At

I have created a class doing some jobs like GridView inherit from System.Web.UI.WebControls.WebControl.

public class IHGridView : System.Web.UI.WebControls.WebControl
{
    // inside here, actually return Repeater class.


    protected override void OnInit(EventArgs e)
    {
        _repeater.ItemTemplate = new IHGridItemTemplate(ListItemType.Item, this.Columns);
        this.Controls.Add(_repeater);
    }
}

I also created ItemTemplate for my repeater in IHGridView.

public class IHGridItemTemplate : ITemplate
{
}

IHGridView class returns Repeater and some html codes, but in convenience to deveop I have created some stuff.

public class Columns : StateManagedCollection
{
}

public class IHBoundFieldBase : IStateManager
{
}

public class IHLabelField : IHBoundFieldBase
{
}

Now in my aspx, I can use this like below:

<cc1:IHGridView ID="IHGridView1" runat="server" EditMode="View">
    <Columns>
         <cc1:IHLabelField ID="IHLabelField7" DataField="PERSON_NAME" HeaderText="PersonName" />
    </Columns>
</cc1:IHGridView>

Now I come up with a problem. I cannot use DataBinder.Eval in aspx.

<cc1:IHLabelField ID="IHLabelField7" HeaderText="PersonName" Text='<%# DataBinder.Eval(Container.DataItem, "PERSON_NAME") %>' />

This gives me an error. The error message is below: CS1061: There is no definition of 'DataItem' in 'System.Web.UI.Control'. There is no extendable method 'DataItem' in 'System.Web.UI.Control''s first argument. Please check if there is using rubric or assembly reference. This was written in Korean, but I translated into English. Could anyone give me a clue to solve this problem?

2

There are 2 answers

0
VinayC On BEST ANSWER

In templated controls, the template is instantiated in the container. For data-binding to work in the templated fields, its recommended that container should implement IDataItemContainer interface - the interface implementation should be supplying the data-item.

AFAIK, to support data binding expressions, ASP.NET parser injects handler for DataBinding event for the control (whose properties uses these expressions) and then in the handler, it generates code that looks for data-item in the container.

So in your example, if you wish to use data-binding expression in the IHLabelField.Text property then the control's naming container should either implement IDataItemContainer or should have DataItem property. So in this case, you will probably need DataItem on IHGridView control - and it wouldn't work the way you want.

0
AudioBubble On

here is an example we used. i hope it helps

   <asp:HyperLink ID="phoneManagementHyperLink" runat="server" Text='<%# (Container.DataItem as WcfUser).firstName + " " + (Container.DataItem as WcfUser).lastName%>'