468,549 Members | 2,344 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

Label within Datalist

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.

Nov 19 '05 #1
9 2385
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>

-Brock
DevelopMentor
http://staff.develop.com/ballen
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.


Nov 19 '05 #2
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.

Nov 19 '05 #3
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'.

jj******@gmail.com wrote in news:1117074201.620317.134830
@z14g2000cwz.googlegroups.com:
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.


Nov 19 '05 #4
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).

Nov 19 '05 #5
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 ;)
jj******@gmail.com wrote in news:1117142523.707340.183010
@g43g2000cwa.googlegroups.com:
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).


Nov 19 '05 #6
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.

Nov 19 '05 #7
You should then index into the DataList's Items collection, not the Controls
collection.

-Brock
DevelopMentor
http://staff.develop.com/ballen
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.


Nov 19 '05 #8
I am not sure Try the same code in the ItemCreated event of the
DataList

-Ram

Nov 19 '05 #9
Try This:
Protected Sub dl_onitemdatabound(ByVal sender As System.Object, ByVal e As DataListItemEventArgs)

ctype(e.item.findcontrol("lblprice"), label).text= "bla.."
May 2 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dude | last post: by
3 posts views Thread by David Kenneally | last post: by
2 posts views Thread by Andy Sutorius | last post: by
1 post views Thread by Danny Tuppeny | last post: by
3 posts views Thread by Danny Tuppeny | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.