asp.net - 为什么不能在 ListView 中使用 <%=...%>

标签 asp.net data-binding

所有,一般。如果我们想输出页面中的变量,该变量定义在页面的代码隐藏类文件中。我们可以用 public 设置变量或 protect能见度。在首页我们可以像下面这样使用它。 <%=VariableName%> .

但是不知道为什么不能在ListView中使用。请帮助审查我的代码。

在首页

<div id="divIssuedListContainer" class="myIssuedList">
<asp:ListView id="listShower" runat="server">
    <LayoutTemplate>
        <div id="divIssuedListHeadTitle">
            <div id="divIssuedListTitleIco">
                <img alt="" src="ico/IssuedCap.png" />
            </div>
            <div id="divIssuedListTitleName">
                <span>ISSUED</span><span>TRADE NAMES</span><span>/LICENSES</span>
            </div>
            <div id="divIssuedListItemNumber">
                <span>
                    <%=iListNumber%></span>
            </div>
        </div>
        <div style="clear:both"></div>
        <div id="divIssuedListBody">
            <ul>
                <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
            </ul>
        </div>
    </LayoutTemplate>
    <ItemTemplate>
        <li>
            <table>
                <tr>
                    <td>
                        <img alt="" src="" />
                    </td>
                    <td>
                        <div>
                            <span>
                                <%# Eval("altID") %>
                                -
                                <%# Eval("englishTradeName") %></span></div>
                        <div>
                            <span>Expired Date:<%# Eval("expDate") %></span>
                        </div>
                    </td>
                </tr>
            </table>
        </li>
    </ItemTemplate>
</asp:ListView>

在页面的代码隐藏中

public partial class _Default : System.Web.UI.Page
    {
        protected int iListNumber = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            List<SimpleCapModel> DataSourceList = TestDataHelper.GetMyIssuedList();
            listShower.DataSource = DataSourceList;
            iListNumber = DataSourceList.Count();
            listShower.DataBind();
        }
    }

我收到一个错误页面说:

 The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Source Error:


Line 19:             listShower.DataSource = DataSourceList;
Line 20:             iListNumber = DataSourceList.Count();
Line 21:             listShower.DataBind();//this line cause error.
Line 22:         }
Line 23:     }

如果我使用表达式 <%#iListNumber%> .错误消失了,但我得到一个空值而不是列表的编号。请查看下面呈现的结果 html:

<div id="divIssuedListItemNumber">
<span> </span>
</div>

不知道是不是漏了什么?如果我这样做了。请帮助让我知道。谢谢。

最佳答案

您需要使用 <%#iListNumber %>因为您使用的是数据绑定(bind) ListView,否则您将得到异常(有关内联表达式的更多信息,请参阅:http://support.microsoft.com/kb/976112)。

尝试使用 <%#iListNumber.ToString %>看看这是否有所作为。

更新:

问题似乎是您的内联表达式在 LayoutTemplate 中,如果没有 either handling the OnLayoutCreated event,数据绑定(bind) [显然] 将无法工作,或扩展 ListView .

有关如何扩展 ListView 的示例,请参阅以下任一示例:

关于asp.net - 为什么不能在 ListView 中使用 <%=...%>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909037/

相关文章:

c# - 自定义控件中的 WPF 数据绑定(bind)

asp.net - 在用户控件中查看状态

c# - 使用 ImageGen 时出现类型或 namespace 错误

c# - 将字符串转换为 int 然后乘以另一个 int

c# - 控制不立即使用 INotifyPropertyChanged 更新绑定(bind)属性

保存前 WPF 数据绑定(bind)

asp.net - 我可以使用 Razor 提供 .html 文件,就好像它们是 .cshtml 文件一样,而不更改所有页面的扩展名吗?

c# - 在预编译项目/库的其余部分时动态地在App_Code中编译一个类

wpf - 尽管属性值正在更新,但在数据绑定(bind)属性的设置 block 中没有命中断点(和额外代码)

c# - 未处理的 GridView 'GridView1' 触发事件排序