how to programmatically assign text to a label control within a datalist template?

G

Guest

I have a label control that I've embedded in a datalist template. I will be
binding data to that label.

I want to run a string formatting function on the database text before it is
injected into the datalist. When I try to do this, I get a "... [control]
does not exist in the current context.

I need to know how to target the control. If I were writing Flash
actionscript, it would be something like

_root.myDatalist.myLabelInsideMyDatalist.Text =
processedTextThatIAlreadyRanThroughMyFunction

Is this possible? Am I running into a page lifecycle thing, or?

Thanks for any help. Actual code follows. I'm trying to inject the formatted
variable text into lblCon_Body.

-KF
<asp:DataList runat="server" id="DataList1" DataKeyField="Con_ContentID"
DataSourceID="SqlDataSource1" Width="100%" HorizontalAlign="Left"
CellPadding="0" >

<ItemTemplate>

<asp:Label Text='<%# Eval("Con_Title") %>' runat="server"
id="Con_TitleLabel" Font-Bold="True" Font-Size="Medium" />

<br />

<asp:Label Text='<%# Eval("Con_BylineCredit") %>' runat="server"
id="Con_BylineCreditLabel" Font-Italic="True" Font-Size="X-Small"
style="font-size: small; line-height: 15pt; font-style: italic; font-family:
Arial, Verdana, Sans-serif" />

<br />

<br />

<asp:Label Text='<%# Eval("Con_Body")%>' runat="server" ID="lblCon_Body"
Font-Italic="False" Font-Size="X-Small" style="font-size: small;
line-height: 15pt; font-style: italic; font-family: Arial, Verdana,
Sans-serif" /><br />

<br />

</ItemTemplate>

</asp:DataList>
 
G

Guest

Yes, you can do that, but you have to get a reference to that control first,
using FindControl(id) and casting it as a label.

Here's an example:

foreach(DataListItem item in MyDataList.Items){
((Label)item.FindControl("lblCon_Body")).Text =
processedTextThatIAlreadyRanThroughMyFunction;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top