Label within Datalist

J

jjack100

I want to set the text value of a label that is within a datalist in
the codebehind. Does this have to be done in the databound event?

Simplified example:

<asp:DataList id="dlItem" runat="server">
<ItemTemplate>
<asp:Label ID="lblItemPrice" runat="server" />
</ItemTemplate>
</asp:DataList>

in codebehind:

Label lblPrice = (Label) dlItem.Controls[0].FindControl("lblPrice");
lblPrice.Text = someValue;

I really just want to know how to access the label explicitly.
Basically, the databinding on the datalist is getting values from a
datareader, but the label does not. For layout purposes, however, the
label needs to be nested within the datalist.
 
B

Brock Allen

You can do that, or you can use the databinding syntax:

<ItemTemplate>
<asp:Label ID="lblItemPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"SomeColumn") %>' />
</ItemTemplate>
 
J

jjack100

Well, my point was that the code I listed doesn't work. It gives a
"Specified argument was out of the range of valid values" error. So
I'm trying to figure out the correct syntax for that.
 
L

Lee

Hi ;)

No idea what's happening here, you already seem to have the answer.

I do notice though, that there's a discrepancy. In your aspx, the label has
id 'lblItemPrice' but in the codebehind, you're looking for 'lblPrice'.



(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
 
J

jjack100

Even fixing that typo in my example does not work. Example:

In the aspx:

<form id="form1" runat="server">

<asp:DataList id="dlItem" runat="server">

<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" />
</ItemTemplate>

</asp:DataList>


</form>


In the Codebehind:

string someValue = "test";

Label lblPrice = (Label) dlItem.Controls[0].FindControl("lblPrice");
lblPrice.Text = someValue;

And this is the error I get:

Specified argument was out of the range of valid values.
Parameter name: index
Line 49: Label lblPrice = (Label)
dlItem.Controls[0].FindControl("lblPrice");

Can't figure out what is going on with that. This is asp 2.0 beta 2,
if that makes any difference (can't imagine why).
 
L

Lee

Umm, by
dlItem.Controls[0].FindControl("lblPrice");

aren't you asking to find a control lblPrice, within the child controls
of dlItem.Control [0] ?

Maybe you should be using
dlItem.FindControl ("lblPrice");


Lee ;)


(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
 
J

jjack100

Still doesn't work. Using the exact code I listed above and changing
dlItem.Controls[0].FindControl­("lblPrice"); to dlItem.FindControl
("lblPrice"); gives a "Object reference not set to an instance of an
object." error.
 
B

Brock Allen

You should then index into the DataList's Items collection, not the Controls
collection.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top