为什么我无法在用户控件的转发器中添加文本和 <%# Container.DataItem %>

问题描述 投票:0回答:2

我有一个由 CodeBehind 动态放置的用户控件,如下所示:

    Dim myControl As Control = CType(Page.LoadControl("~/Controls/mainMenu.ascx"), Control)
    If InStr(Request.ServerVariables("url"), "/Login.aspx") <= 0 Then
        mainMenu.Controls.Add(myControl)
    End If

按照我之前的问题中的一个例子。

此控件内有一个中继器,它调用数据库来生成值。

我的中继器标记如下

<asp:Repeater runat="server" ID="locationRepeater" OnItemDataBound="getQuestionCount">                        
    <ItemTemplate>
       <p id='locationQuestions' title='<%# Container.DataItem %>' runat='server'></p>
    </ItemTemplate>
</asp:Repeater>

上面的示例工作正常,但我希望能够在

<%# Container.DataItem %>
> 的标题属性中将文本添加到
<p
中,以打印到浏览器,如
this is some text DATA_ITEM_OUTPUT

当我尝试这样做时,它会像那样打印

this is some text <%# Container.DataItem %>
,即将
<%# Container.DataItem %>
转换为文本,而不是转发器代码中的值。

在我将其制作为动态插入控件之前,它工作得很好,所以我想我可能会以错误的顺序生成一些东西,但鉴于它在没有任何前置文本的情况下工作,我很难修复它!

我是 .net 的新手,正在使用 vb.net,请有人指出我正确的方向吗?

asp.net vb.net repeater
2个回答
0
投票

查看代码的损坏版本会有所帮助,但我猜你正在这样做:

<p id='locationQuestions' title='This is some text <%# Container.DataItem %>' runat='server'></p>

如果是这样的话,请尝试这样:

<p id='locationQuestions' title='<%# "This is some text " & Container.DataItem %>' runat='server'></p>

0
投票

以下语句将服务器端代码包含在您的页面代码中。

 <%# Container.DataItem %>

当您在页面上进行服务器端代码并进行一些操作时,然后将 它在单引号和代码中,就像你在后面的代码一样。

例如

Text = '<%# "Employee Name:" & Eval("EmpName") %>'

所以你的标题标记代码应该是这样的:

title='<%# "This is Title  " & DataBinder.Eval(Container.DataItem, "Name") & "Text." %>'

最重要的是,VB 和 C# 的 Databinder.Eval 语法是相同的。

查看此链接了解更多详情。

© www.soinside.com 2019 - 2024. All rights reserved.