473,802 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="lblItemPric e" runat="server" />
</ItemTemplate>
</asp:DataList>

in codebehind:

Label lblPrice = (Label) dlItem.Controls[0].FindControl("l blPrice");
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 2583
You can do that, or you can use the databinding syntax:

<ItemTemplate >
<asp:Label ID="lblItemPric e" runat="server" Text='<%# DataBinder.Eval (Container.Data Item,
"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="lblItemPric e" runat="server" />
</ItemTemplate>
</asp:DataList>
in codebehind:

Label lblPrice = (Label) dlItem.Controls[0].FindControl("l blPrice");
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.go oglegroups.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("l blPrice");
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("l blPrice");

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("l blPrice");

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

Maybe you should be using
dlItem.FindCont rol ("lblPrice") ;
Lee ;)
jj******@gmail. com wrote in news:1117142523 .707340.183010
@g43g2000cwa.go oglegroups.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("l blPrice");
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("l blPrice");

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.FindCont rol
("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
Tanqueray_Tonic
1 New Member
Try This:
Protected Sub dl_onitemdatabo und(ByVal sender As System.Object, ByVal e As DataListItemEve ntArgs)

ctype(e.item.fi ndcontrol("lblp rice"), 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
1141
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 sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item ||
3
5361
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" onUpdateCommand="myListUpdateHandler" onCancelCommand="myListCancelHandler" onDeleteCommand="myListDeleteHandler" DataKeyField="ID"> <ItemTemplate>
3
3799
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 id="inpFileUp" type="file" runat="Server" />&nbsp;</td> <asp:Button id="Button1" onclick="Button_Click2" Runat="Server" Text="Upload File!"></asp:Button>
2
1238
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 contains items that can be placed in shopping cart. I want the label to notify the user that the placement of the item into the shopping was successful. Thanks,
0
933
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 sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lbl = (Label)e.Item.FindControl("lblStatus");
1
1015
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 like: Tour 1 Gig 1 date Gig 2 date Tour 2
3
1967
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, but the RescheduledFromDate does. In general, only a GigDate will be supplied, but if a Gig is rescheduled, the GigDate is copied to RescheduledFromDate, and the GigDate then becomes the "new" date. I'm using a DataList instead of a Grid because...
0
956
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 also give a possibility to "cancel" and "delete". If the user has modified the data wich returns a false for the used regex validator if also fires OnDelete or OnCancel.
1
2563
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? http://bytes.com/groups/net-asp/327013-label-within-repeater-losing-text-post-back (I don't seem to be able to post to that group or something). Thanks.
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.