AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to get text from a label in a DataList ItemTemplate ?
Posted by Charlie Childs on  4/13/2010 10:50:30 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
Hi,
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:-
[CODE] 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 [/CODE]
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

Post Your Reply Post New Question

 Viewer's Reply
Finding Controls inside DataList
Answered By Moderator1 on 4/17/2010 8:22:23 PM
Hi,

You need to find controls inside the datalist as follows,

foreach (DataListItem lst in DataList1.Items)
{
Label lblInc = (Label)lst.FindControl("lblInc");
lblInc.Text="Find My Label Control";

}
 
Get Label.Text from datalist template to Textbox
Answered By Charlie Childs on 4/18/2010 12:01:38 PM
Good to hear from you moderator one - I managed to sort this as per below but I owe you a word of praise- EXCELLENT!! for another tutorial.


Give the button a CommandName of "Item".

Click on ItemCommand property of DataList to create a protected void. Fill the protected void as follows.

{ 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 + label1.Text;
}
}

Works a treat.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Programmers Heaven C# School Book - Free 338 Page eBook
No Nonsense XML Web Development With PHP - Free 146 Page Preview!
Simply JavaScript - Free 150 Page Preview!