为什么不能使用<%= ...%GT;在ListView

编程入门 行业动态 更新时间:2024-10-23 20:28:36
本文介绍了为什么不能使用<%= ...%GT;在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所有的,一般。如果我们想输出其中的code隐藏页的类文件中定义的页面的变量。我们可以设置与公共或保护知名度变量。并在头版我们可以用它象下面。<%= VARIABLENAME%方式>

All, Generally .If we want to output a variable in a page which defined in the code-behind class file of page . We could set the variable with public or protect visibility. and in the front page we can use it like below. <%=VariableName%>.

但我不知道为什么不能在ListView中使用它。请帮忙审查我的code。

But I don't know why can't use it in the ListView. Please help to review my code.

在头版

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

在页面的code-背后

In the code-behind of page

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(); } }

我得到一个错误网页说:

I got an error page said :

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

如果我使用ex pression &LT;%#iListNumber%GT; 。错误走了,但我得到了一个空值,而不是列表的数量。请查看以下渲染结果的html:

And If I use the expression <%#iListNumber%>. The error is gone, but I got a empty value instead of the number of the list. please review below rendered result html :

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

我不知道我是否错过了什么?如果我这样做。请帮助,让我知道it.thanks。

I didn't know whether I missed something ? If I did .Please help to let me know it.thanks.

推荐答案

您需要使用&LT;%#iListNumber%GT; ,因为你正在使用数据绑定的ListView控件,否则你会得到异常(见: support.microsoft/kb/976112 了解内联前pressions)。

You need to use <%#iListNumber %> because you are using data-binding the ListView, otherwise you'll get the exception (see: support.microsoft/kb/976112 for more on inline expressions).

尝试使用&LT;%#iListNumber.ToString%方式&gt; ,看看是否有差别。

Try using <%#iListNumber.ToString %> and see if that makes a difference.

更新

这个问题似乎是你的直列前pression是在 LayoutTemplate模板,其中数据绑定[显然]将无法正常工作没有either处理 OnLayoutCreated 事件,或延长的ListView 。

The problem appears to be that your inline-expression is in the LayoutTemplate, where data-binding [apparently] won't work without either handling the OnLayoutCreated event, or extending the ListView.

请参阅无论是对如何延长 ListView的例子以下:

See either of the following for examples of how to extend the ListView:

  • 的www.$c$cproject/Articles/42962/Databind-Ex$p$pssion-in-ListView-LayoutTemplate
  • forums.asp/p/1201992/3319344。 ASPX#3319344
  • www.codeproject/Articles/42962/Databind-Expression-in-ListView-LayoutTemplate
  • forums.asp/p/1201992/3319344.aspx#3319344

更多推荐

为什么不能使用&lt;%= ...%GT;在ListView

本文发布于:2023-10-31 04:58:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   GT   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!