Literal in Code behind
why I cant access to literal in behind Code of my asp.net page?
<%@ 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 is unknown element in behind code.
ltlText is unknown directly since it lives in a containing control: your repeater. If you want to get to it you need to iterate over the repeater rows, for example in the ItemDataBound event and there use the FindControl method to find your literal.
Take a look at the sample code in the MSDN documentation: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx.
The code you're after might look something like this:
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";
}
}
This is because the literal is inside a Repeater. Possibly multiple instances of it are created using the template you provide, so merely referring to it by name is not going to work.
Use something like:
Literal ltlText = (Literal)rptHelp.Items[0].FindControl("ltlText");
// Do stuff with literal
Sorry, not awake enough to convert to VB syntax right now, but hopefully you get the idea.
Rather than accessing the items like this, you will probably want to execute your code while the repeater is doing its work, for example in the OnItemDataBound event handler of your control.
Some more C# code of an example event handler:
protected void rptHelp_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
// In this example, the repeater's data source is a DataTable,
// so each item corresponds to a DataRow
DataRow row = e.Item.DataItem as DataRow;
Literal ltlText = (Literal)e.Item.FindControl("ltlText");
// Set literal based on data here
}
}
精彩评论