4 Replies - 14556 Views - Last Post: 16 April 2010 - 08:49 AM Rate Topic: -----

#1 charliechilds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-October 09

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

Posted 13 April 2010 - 09:00 AM

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:-
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

Is This A Good Question/Topic? 0
  • +

Replies To: How to get text from a label in a DataList ItemTemplate ?

#2 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

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

Posted 13 April 2010 - 09:58 AM

Edit: My mistake, posted in wrong topic.

This post has been edited by keakTheGEEK: 13 April 2010 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 gehres  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 28-October 09

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

Posted 14 April 2010 - 08:03 PM

View Postcharliechilds, on 13 April 2010 - 08:00 AM, said:

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:-
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


I believe you will have to include a select button or link within your datalist template so you can have access to the selected item within the list. Take a look at this markup and that should give you some ideas.

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" Width="85%" 
	EnableTheming="False"  RepeatDirection="Horizontal">	

	<ItemTemplate>	
							
								<%#DataBinder.Eval(Container.DataItem, "Title")%>	
							
			
				<a href='http://somesite.com/Default.aspx?Part_Number=<%# DataBinder.Eval(Container.DataItem, "ProductId") %>' >			
				<asp:Image ID="Image2"  runat="server" Height="150" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "ImageURL")%>'
					Width="150"  ToolTip="Click to view details" alt="Ltd. Product"/>					
					</a>
			
										
								ItemId:<%#DataBinder.Eval(Container.DataItem, "ProductId", "{0:c}")%>			<br />
								<b>Price:<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %></b><br />
									Quanity Available: <%#DataBinder.Eval(Container.DataItem, "QTYOH", "{0}")%>
							
				
		
					
				<a href='http://somesite.com/Checkout.aspx?AddToCart=<%# DataBinder.Eval(Container.DataItem, "ProductId") %>&amp;<%# Request.QueryString%>'>
					<img border="0" src="http://somesite.com/App_Themes/Addtocart btn1.png"  alt="add to cart"/ >
				
			
				
	</ItemTemplate>
</asp:DataList>


Was This Post Helpful? 0
  • +
  • -

#4 charliechilds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-October 09

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

Posted 15 April 2010 - 09:37 AM

View Postgehres, on 14 April 2010 - 07:03 PM, said:

View Postcharliechilds, on 13 April 2010 - 08:00 AM, said:

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:-
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


I believe you will have to include a select button or link within your datalist template so you can have access to the selected item within the list. Take a look at this markup and that should give you some ideas.

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" Width="85%" 
	EnableTheming="False"  RepeatDirection="Horizontal">	

	<ItemTemplate>	
							
								<%#DataBinder.Eval(Container.DataItem, "Title")%>	
							
			
				<a href='http://somesite.com/Default.aspx?Part_Number=<%# DataBinder.Eval(Container.DataItem, "ProductId") %>' >			
				<asp:Image ID="Image2"  runat="server" Height="150" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "ImageURL")%>'
					Width="150"  ToolTip="Click to view details" alt="Ltd. Product"/>					
					</a>
			
										
								ItemId:<%#DataBinder.Eval(Container.DataItem, "ProductId", "{0:c}")%>			<br />
								<b>Price:<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %></b><br />
									Quanity Available: <%#DataBinder.Eval(Container.DataItem, "QTYOH", "{0}")%>
							
				
		
					
				<a href='http://somesite.com/Checkout.aspx?AddToCart=<%# DataBinder.Eval(Container.DataItem, "ProductId") %>&amp;<%# Request.QueryString%>'>
					<img border="0" src="http://somesite.com/App_Themes/Addtocart btn1.png"  alt="add to cart"/ >
				
			
				
	</ItemTemplate>
</asp:DataList>



Hi Gehres,

Thanks for your feedback - I'm playing with ItemCommand at present but will be looking into this. Thanks !!
Was This Post Helpful? 0
  • +
  • -

#5 charliechilds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-October 09

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

Posted 16 April 2010 - 08:49 AM

SORTED!

Thanks for the offer Gehres. A friend helped do the following.

Created an ItemCommand event by clicking on Item Command in the datalist properties events. Gave the button a CommandName of "Item".
Then added an If . . .as followes

If e.CommandName = "Item" Then
Dim label1 As Label = DirectCast(DataList1.Items(e.Item.ItemIndex).FindControl("Label1"), Label)
Dim label2 As Label = DirectCast(DataList1.Items(e.Item.ItemIndex).FindControl("Label2"), Label)

TextBox.Text = label1.Text

End If
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1