开发者

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

I have a user control (ascx) that I have added two public properties to: RequestTypeId and GroupId. Both have the Bindable(True) attribute set.

In my aspx page, I have a ListView, and in the ItemTemplate, I place my control reference, like so:

    <asp开发者_StackOverflow:ListView ID="lv" runat="server">
        <LayoutTemplate>
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
        </LayoutTemplate>
        <ItemTemplate>
           <tr>
              <td>
                 <ctrl:ServiceTypeGroup id="ctrlServiceTypes" runat="server" RequestTypeId="<%#RequestType.RequestTypeId%>" GroupId='<%#Eval("Id")%>' />
              </td>
           </tr>
        </ItemTemplate>
    </asp:ListView>

Setting the RequestTypeId works fine. Setting the GroupId fails with the following error:

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

What do I need to do to my user control code to allow binding a Eval() expression to one of its properties? Or is it not possible?


There are some properties such as [Bindable(true)] which you can give to user controls.

Have you added this to the properties in question? It would look something like this:

[Bindable(true)]
public int RequestTypeID { get; set; }

[Bindable(true)]
public int GroupID { get; set; }

You will need the following at the top:

using System.ComponentModel;

More information:

  • http://msdn.microsoft.com/en-us/library/system.componentmodel.bindableattribute.aspx
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜