473,231 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 2554
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DKode | last post by:
Ok, This seems like it should be simple, but everytime I access the label, it's text property is always set to "". Here is the code I am using: private void...
3
by: Dude | last post by:
Below is the code - it is finding the control, and there is no error, just not assigning the text to the label <asp:DataList id="dlGoals" runat="server" onEditCommand="myListEditHandler"...
3
by: David Kenneally | last post by:
Hello- I'm having a problem doing a file upload from within the edit function of a datalist. I have a simple file upload that works on a standard ..net page: <td width="53"><input...
2
by: Andy Sutorius | last post by:
Hi, Are datalists able to have labels inside of the ItemTemplate? Every time I try to place a label in the Item Template it does not show up in the code behind/intellisense. FYI the datalist...
0
by: DKode | last post by:
Ok, This seems like it should be simple, but everytime I access the label, it's text property is always set to "". Here is the code I am using: private void dgrComputer_ItemDataBound(object...
1
by: Danny Tuppeny | last post by:
Hi all, I've got two tables, Tour and Gig. The Gig table has a TourID, to give a one-many relationship. I'm currently displaying a whole list of Gigs without tours, but I'd like to display it...
3
by: Danny Tuppeny | last post by:
Hi all, I've got a DataList that's bound to a datasource with two columns (well, two that matter). One is called GigDate, and one is called RescheduledFromDate. GigDate doesn't allow NULLs,...
0
by: MA | last post by:
Hi, I'm strugling with tho following case: I use a datalist in my asp.net application to present an interface to the use where he/she can modify some database tables. In de "update mode" I...
1
by: supraracer | last post by:
Can anyone show me an example of a solution to the problem described in this thread? How do I rebind a label in a repeater on a postback? ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.