473,320 Members | 1,856 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,320 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 2555
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? ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.