how to get the text of label in Datalist?

A

Arvan

hi,all.

i placed a datalist named DataList1 and placed a Label named Label1 in item
template.

how do i get the text of control Label?

code:

<asp:DataList ID="DataList1" RepeatColumns="4" runat="server"
RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>
 
N

Nathan Sokalski

Normally when you want to do that you are doing it from within an event
raised by the DataList. I will assume that is where you are doing this from.
Here is a line of code that will get the Text property of the current Item:

CType(e.Item.FindControl("lblMyLabel"), Label).Text

If you needed to access this data from a method/function other than one of
the events raised by the DataList, you will need to know the index of the
item the desired Label is in. In this case, use the following:

CType(DataList1.Items(index).FindControl("lblMyLabel"), Label).Text

Please remember that when accessing data this way you must do it before
calling the databind() method in the case of databound properties (it
doesn't look like your ItemTemplate has any databound properties, but I
assume it will), otherwise it will return a value of "". Good Luck!
 
A

Arvan

Thank you very much. :)
Nathan Sokalski said:
Normally when you want to do that you are doing it from within an event
raised by the DataList. I will assume that is where you are doing this
from. Here is a line of code that will get the Text property of the
current Item:

CType(e.Item.FindControl("lblMyLabel"), Label).Text

If you needed to access this data from a method/function other than one of
the events raised by the DataList, you will need to know the index of the
item the desired Label is in. In this case, use the following:

CType(DataList1.Items(index).FindControl("lblMyLabel"), Label).Text

Please remember that when accessing data this way you must do it before
calling the databind() method in the case of databound properties (it
doesn't look like your ItemTemplate has any databound properties, but I
assume it will), otherwise it will return a value of "". Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top