微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.net – 代码背后的文字

为什么我无法在我的asp.net页面代码后面访问文字

<%@ Page Title="" Language="VB" MasterPageFile="~/UI/Masters/Window.master" AutoEventWireup="false" CodeFile="HelpViewer.aspx.vb" Inherits="UI_Pages_HelpViewer" culture="auto" Meta:resourcekey="PageResource1" uiculture="auto" %>

<asp:Content ID="Content1" ContentPlaceHolderID="c" Runat="Server">
<%--<div dir="rtl">
    <asp:Panel ID="Panel1" Height="270px" Width="100%" ScrollBars="Auto" 
        runat="server" Meta:resourcekey="Panel1Resource1">
       <asp:Literal ID="Literal1" runat="server" Meta:resourceKey="Literal1Resource1"></asp:Literal>
 </asp:Panel>
</div>--%>

<div dir="rtl" align="right">
        <asp:Repeater ID="rptHelp" runat="server" DataSourceID="xmlHelp">
            <ItemTemplate>
                <div style ="font-size:12px; font-family :Tahoma; font-weight:bold; margin-left:5px; color:Green; ">
                      <asp:Literal ID="ltlTitle" runat="server" Text='<%#XPath("title")%>'></asp:Literal>
                </div>
                <div style="font-size:11px;margin-bottom:10px; margin-left:12px; margin-right:4px; font-family:Tahoma ; margin-top:9px;">
                    <asp:Literal ID="ltlText" runat="server" Text='<%#XPath("text")%>'></asp:Literal>
                </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:XmlDataSource ID="xmlHelp" runat="server"></asp:XmlDataSource>
    </div>
</asp:Content>

ltlText是后面代码中的未知元素.

解决方法

ltlText是直接未知的,因为它存在于一个包含控件中:你的转发器.如果你想要它,你需要迭代转发器行,例如在 ItemDataBound event中,并使用FindControl方法来查找你的文字.

请查看MSDN文档中的示例代码http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx.

您所追求的代码可能如下所示:

rptHelp_ItemDataBound(Object Sender,RepeaterItemEventArgs e) 
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

        Literal lt = (Literal)e.Item.FindControl("ltlText");
        lt.Text = "Test";
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐