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

发布于 2024-08-29 03:54:34 字数 962 浏览 2 评论 0原文

<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);
%>
<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>

The following error

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

is coming in this part

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-09-05 03:54:34

在这种情况下,您需要像这样的完整调用:

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

In that context you need the full call like this:

<%#  Databinder.Eval(Container.DataItem,"StudentUserName") %>
魂归处 2024-09-05 03:54:34

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

<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 + " ]";
    }
}

Alright guys, thanks for the help, but i got the solution :

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

and in the code behind file, I wrote the following function :

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 + " ]";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文