How to get text from a label in a DataList ItemTemplate?

I use Visual Web Developer 2008 Express.
I have Label1 in a DataList ItemTemplate. I wish to use a button to get the text from that label and add it to text in TextBox1 which is elsewhere on the page.

I know how to do this from a standard label on a ASP.NET web page but cannot work out how to get the text from a label inside a DataList ItemTemplate.

This is what I thought I should do:-

Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
TextBox1.Text = TextBox1.Text & " " & DataList1.ItemTemplates.ItemTemplate.Label2.Text
End Sub 

I thought that because “ DataList1.ItemTemplates.ItemTemplate.Label2 - System.Web.UI.WebControls.Label “ - is what is written in the properties window header when I select the label in the DataList template.

When I try that, I get a Blue Squiggle under “DataList1.ItemTemplates” and if I hover over the squiggle the error box comes up saying " ‘ItemTemplates’ is not a member of ‘System.Web.UI.WeControls.DataList’

Thanks for any help on offer


Well, the problem is that your item template will be repeated and each item will contain the label. How do you determine which label gets used?

However you do, what you need to do is loop through your datalist.

foreach (DataListItem dli in DataList1.Items)
    Label Label2 = (Label)dli.FindControl("Label2");

    TextBox1.Text = TextBox1.Text + " " + Label2.Text;

This is C# btw, VB should be the same without the ; and replace + with &.

I hope this helps

Hi NightStalker,

Those fangs are looking meaner every time I see them - but I can tell you are a nice vampire.

I have a datalist delivering many different images to the page. Each picture is an image button. Each image has a name underneath it which is the lablel. Both image button and label are databound.
I want to click the image button to send the text from its label to textbox somewhere else on the page. So there are many different pictures all with there own corresponding label. So if picture is of Vampires, its label underneath says “Vampires” as its text. If another picture is of Frankenstein the label underneath it says “Frankenstein”.

The idea is for user to click on different images and end up with a list of text which is a copy of each label text. So if user clicks Vampire image and then clicks Frankenstein image the textbox elsewhere on the page will show:


Thanks very much for the code - I will try this out a bit later. I am ussuming that it gets placed inside the sub routine which I posted earlier in the thread.
I will let you know how I get on.
Thanks again :wink:

hahaha. ok.

If the button is inside a datalist item, a normal click event will not work. You will need a ItemCommand event that fires on click. Then in there, you can get the label from the same row. Dnt have time now to post code to help. I will post when I get back if u havent figured it out by then yet.


I think you may be just a little optimistic about me figuring this out myself but I appreciate the hint re ItemCommand and the nod to try.

I have got as far as finding the ItemCommand button in the events section of the DataList properties. I have also discovered a “Command Name” in the Image Button properties and in the events section of the Image Button properties I discovered a “Command”. What to do with it all, if those discoveries are along the right lines, is another matter ??

I gave the image button a command name of AddToList and hoped it would pop up in the ItemCommand of the DataList events which sort of made sense to me - but no such luck there.

I would be very grateful for further guidence.

Yes, you are getting there. To create the method in design view. Select the datalist, Go to the properties window and go to events. You will find an ItemCommand. Double click next to it, and visual studio will create the references to it from the aspx and also create the method definition on the backend.

IIRC, you also need to set your datakeys field of the datalist.(This might only be for gridview tho) I havnt used datalist in a long time. I will have to see the method signature b4 I can tell you how to do it from there.

But within that method with the help of the supplied arguments, you should be able to get what you want. Try and play around a bit. If not luck, post your code and I can asist from there.

I double clicked the Datalist’s ItemCommand and I got the Potected Sub in the code behind ( is that the “argument” ?) - but - I cant see any
“reference” on the aspx page ( would that be the “method” ) - I search aspx page for “ItemCommand” and no joy and dont see any new html apart from the CommandName=“AddToList” which I told you I added to the image button properties earlier.
So a little confused about the reference I am expecting on the aspx page and if it would be the method you refer to.

In your final paragraph, you start by saying " But within that method" just to be crystal - I currently cant rule out that you may mean the code you sent me earlier??

If its any encouragement to you re helping me - my practical results are quite good so far :
Database+datalist+filters -
Wicked pager -
And uploading etc going well - it the beauty of the wisiwag verses the ignorance of the wisiwag that I am suffering from.

The reference will be an attribute in your datalist. OnItemCommand=“sub_name”. That is all that is needed for the reference.

When I say with in that method, i mean the one the visual studio creates. Paste the methods signature here, then I can help u a bit further with the code

Hi NightStalker,

I have searched the aspx page for this reference but no joy.

DataList code on aspx page as follows:

<asp:DataList ID="DataList1" runat="server"
        RepeatColumns="4" RepeatDirection="Horizontal" CellSpacing="29"
        GridLines="Both" HorizontalAlign="Justify" Width="660px">
<EditItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
             <asp:ImageButton ID="ImageButton1" runat="server"
                ImageUrl='<%# Eval("ImgUrl") %>'
                onclick="ImageButton1_Click" />
            <br />
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("ProductCode") %>'></asp:Label>
            <br />
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
            <br />

Code behind as follows:

       Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
    End Sub

No major rush - Exploring the events etc is interesting. I’m very grateful for help when you can manage it.:slight_smile:

ok, well I can only give u the code in C#. As I am not 100% sure of the vb syntax, havent used it in AGES!

Ok, first change ur image button to:

<asp:ImageButton ID="ImageButton1" runat="server" 
                ImageUrl='<%# Eval("ImgUrl") %>'  
                CommandName="Item" />

then in the method you gave me you can use these to lines to get reference to the labels.

if (e.CommandName=="Item") //Just to make sure its from the click of the image button. But there is only one command, so its not needed. Just for safety
Label label1 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label1");
Label label2 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label2");

then u can just use it like a normal label. Just use the intelli sense for the above code as I have not used a datalist in a long time either.

Good luck

Thanks very much for your help. The datalist pager I use is in C# so the page is going that way anyway. I think I have enough to play with to find a way through - I will post here when I have cracked it - it may be a few days.
Thanks again!!

Not quite there yet.

I’m using C# and have been trying ever since last post. I have your code sitting in brackets in my code behind with no squiggly lines. From what I can tell that code indexes the labels and tells PC to make them available when the image buttion is clicked.

But I cant work out the path to the label.text - I have tried many various ways including most of what intellisence suggests. I get squiggly lines when ever I use the word “Label1” or “label1” which I think means that I am not reaching into the datalist template ( there are no other labels on the page ).

TextBox2.Text = TextBox2.Text + Datalist1. ?? SelectedItemTemplate makes most sense to me ? but cant make it work ? ? ?? - Am I wrong to assume that the this sequence should be in the ImageButton_Click event.

Please advise if you have time. I do have 2 other problems to solve which I can move to for releif but very nearly finished business is making it very difficult to put this down - I am cancer the crab and over tenacious - I go :injured: before I give in and go :mad: before I accept defeat I go er, um :x on a bit too much sometimes

No, you must not put it in the ImageButton click event. You cannot think of controls in a databound controls as the same if they are out. Buttons in datalists do not have click events. They fire the ItemCommand of the datalist. That is where ur code needs to be, and you need to make a reference to ur labels. Whether there are 10 or only 1 item in the datalist, you still need to reference it with the code that I gave you

Thanks for that - I thought I was getting close when I was trying it that way earlier but for some stupid reason I thought I reverted back to the image button event.

By the way I have just discovered an old thread of yours re paging a datalist from 2006 - its been quite useful re steps towards getting me started on my way to a solution for somethings else i am trying to sort.


This looks the closest I have been but I need an identifier before COLOR=“Red”[/COLOR]. There is no intellisense at that point. Googled “identifier” but several meanings - not very helpful. Can you offer any assistance?

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)

{ if (e.CommandName == “Item”)
Label label1 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl(“Label1”);
Label label2 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl(“Label2”);
TextBox2.Text = TextBox2.Text + " " + DataList1.ItemTemplate.(label1.Text);

Grateful for any advice you can offer :slight_smile:

You not using it like a normal control at that point. Once you have made reference to it, u use it like normal. So:

TextBox2.Text = TextBox2.Text + " " + DataList1.ItemTemplate.(label1.Text);


TextBox2.Text = TextBox2.Text + " " + label1.Text;

I should have known. It works a treat.
Thanks for encouragement / time and effort.:vampire::vampire::vampire::slight_smile:

And to round off this thread caraige return as follows:

TextBox2.Text = TextBox2.Text + label1.Text + " " + label2.Text + Environment.NewLine;