3

全部,一般来说。如果我们想在 page 的代码隐藏类文件中定义的页面中输出一个变量。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>

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

4

2 回答 2

2

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

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

更新

问题似乎是您的内联表达式位于 中LayoutTemplate,如果不处理OnLayoutCreated事件或扩展ListView.

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

于 2013-06-04T03:25:22.487 回答
0

鉴于您收到的错误,它基本上意味着:您不能只是在数据绑定控件中随机抛出 .NET 标记。

解决方案是,将随机文本放入 .NET 控件中,如下所示:<asp:Literal Runat="server" Text="<%# iListNumber %>" />

当您将 .NET 代码块放入代码中时,它必须动态地为其构建一个控件占位符。这有点棘手。但是,如果您将所有自定义标记包装在一个控件中,那么只有一个属性会被数据绑定,并且如果有意义的话,数据绑定块将保持“干净”。

于 2013-06-04T03:40:18.907 回答