在asp.net页面上的服务器端代码中使用eval

时间:2010-04-20 02:26:49

标签: asp.net asp.net-membership

<asp:Repeater ID="rptrParent" runat="server">
<ItemTemplate>
        <li>
            <a href="<% =ResolveUrl("~/cPanel/UserView.aspx?User=")%><%# Eval("StudentUserName") %>">
                <span>
                    <%  ProfileCommon pc = new ProfileCommon();
                        pc.GetProfile(Eval("StudentUserName").ToString());
                        Response.Write(pc.FirstName + "" + pc.LastName);
                    %>
                </span>
            </a>
        </li>
</ItemTemplate>

以下错误

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

正在进入这一部分

<%  ProfileCommon pc = new ProfileCommon();
    pc.GetProfile(Eval("StudentUserName").ToString());
    Response.Write(pc.FirstName + "" + pc.LastName);
%>

2 个答案:

答案 0 :(得分:4)

在这种情况下,你需要像这样的完整电话:

<%#  Databinder.Eval(Container.DataItem,"StudentUserName") %>

答案 1 :(得分:0)

好的伙计们,感谢您的帮助,但我得到了解决方案:

<asp:Repeater ID="rptrParent" runat="server" onitemdatabound="rptrParent_ItemDataBound">
<ItemTemplate>
        <li>
            <a href="<% =ResolveUrl("~/cPanel/UserView.aspx?User=")%><%# Eval("StudentUserName") %>">
                <span>
                    <asp:Literal ID="lblUserName" runat="server"></asp:Literal>
                </span>
            </a>
        </li>
</ItemTemplate>

在代码隐藏文件中,我编写了以下函数:

protected void rptrParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        Literal UserName = e.Item.FindControl("lblUserName") as Literal;
        String uName = DataBinder.Eval(e.Item.DataItem, "StudentUserName").ToString();
        ProfileCommon pc = Profile.GetProfile(uName);
        UserName.Text = pc.FirstName + " " + pc.LastName + " [ " + uName + " ]";
    }
}