473,382 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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:-
Expand|Select|Wrap|Line Numbers
  1.    Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
  2. TextBox1.Text = TextBox1.Text & " " & DataList1.ItemTemplates.ItemTemplate.Label2.Text
  3. 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
Apr 13 '10 #1
2 7275
CroCrew
564 Expert 512MB
Hello CharlieChilds,

I personally find easier to use a datagrid. Here is a working example that you can try out. I believe that it meets your requirements. Let me know if you have any questions.

Default.aspx
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" EnableEventValidation="false" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.     <head id="Head1" runat="server">
  5.         <title></title>
  6.     </head>
  7.     <body>
  8.         <form id="form1" runat="server">
  9.             <div>
  10.                 Selected Name: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  Age: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  11.  
  12.                 <asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" GridLines="Both">
  13.                     <Columns>
  14.                         <asp:BoundColumn DataField="KeyId" Visible="false"></asp:BoundColumn>
  15.                         <asp:BoundColumn DataField="FullName" Visible="true"></asp:BoundColumn>
  16.                         <asp:BoundColumn DataField="Age" Visible="true"></asp:BoundColumn>
  17.                         <asp:ButtonColumn CommandName="Copy" DataTextField="SelectButton"></asp:ButtonColumn>
  18.                     </Columns>
  19.                 </asp:DataGrid>                
  20.             </div>
  21.         </form>
  22.     </body>
  23. </html>
  24.  

Default.aspx.vb
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2.  
  3. Partial Class Default
  4.     Inherits System.Web.UI.Page
  5.  
  6.  
  7.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8.         FillDataList()
  9.     End Sub
  10.  
  11.     Sub FillDataList()
  12.         Dim mydatatable As New DataTable
  13.         mydatatable.Columns.Add("KeyId", Type.GetType("System.String"))
  14.         mydatatable.Columns.Add("FullName", Type.GetType("System.String"))
  15.         mydatatable.Columns.Add("Age", Type.GetType("System.String"))
  16.         mydatatable.Columns.Add("SelectButton", Type.GetType("System.String"))
  17.  
  18.         Dim myrow As DataRow
  19.  
  20.         myrow = mydatatable.NewRow
  21.         myrow("KeyId") = 1
  22.         myrow("FullName") = "Izabella Miko"
  23.         myrow("Age") = 29
  24.         myrow("SelectButton") = "<input id='Button1' type='button' value='Select' onclick=""__doPostBack('DataGrid1$ctl02$ctl00')"" />"
  25.         mydatatable.Rows.Add(myrow)
  26.  
  27.         myrow = mydatatable.NewRow
  28.         myrow("KeyId") = 2
  29.         myrow("FullName") = "Tiffani-Amber Thiessen"
  30.         myrow("Age") = 36
  31.         myrow("SelectButton") = "<input id='Button1' type='button' value='Select' onclick=""__doPostBack('DataGrid1$ctl03$ctl00')"" />"
  32.         mydatatable.Rows.Add(myrow)
  33.  
  34.         myrow = mydatatable.NewRow
  35.         myrow("KeyId") = 3
  36.         myrow("FullName") = "Mischa Barton"
  37.         myrow("Age") = 24
  38.         myrow("SelectButton") = "<input id='Button1' type='button' value='Select' onclick=""__doPostBack('DataGrid1$ctl04$ctl00')"" />"
  39.         mydatatable.Rows.Add(myrow)
  40.  
  41.  
  42.         DataGrid1.DataSource = mydatatable
  43.         DataGrid1.DataBind()
  44.     End Sub
  45.  
  46.     Protected Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
  47.         Select Case (e.CommandName)
  48.             Case "Copy"
  49.                 TextBox1.Text = e.Item.Cells(1).Text.ToString
  50.                 TextBox2.Text = e.Item.Cells(2).Text.ToString
  51.             Case Else
  52.                 ' You can add more cases for other functions.
  53.         End Select
  54.     End Sub
  55. End Class
  56.  
This is what I call a “hard” example. If I were to write it into my application many things would be done dynamically like the data binding and the onclick event would be in a loop.

Happy Coding,
CroCrew~
Apr 15 '10 #2
Hi CroCrew,

Thanks very much for your response.

I don't see dataGrid in my tool box of VWD 2008 Express. I used datalist because I need to repeat images accross the page. I tried GridView first using different fields for image urls but then realised I had a problem with databinding to other field info per individual image.
I am still not 100% sure that DataList can do all that I need but so far so good.

Have sorted this posts issue out by using ItemCommand - as follows:

Give the button a CommandName of "Item".

Click on ItemCommand property of DataList to create a protected void. Fill the protected void as follows.
Expand|Select|Wrap|Line Numbers
  1.     protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
  2.     {  if (e.CommandName == "Item")
  3.         {
  4.             Label label1 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label1");
  5.             Label label2 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label2");
  6.             TextBox2.Text = TextBox2.Text + label1.Text + " " + label2.Text + Environment.NewLine;
  7.         }
  8.     }
It works a treat.
Thanks again for your effort and advice. I'll be looking up DataGrid
Apr 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ben R. | last post by:
I've got a repeater that's bound to a datareader and I'd like to conditionally modify the text that is being outputted. Previously, I've done this by assigning a name to attribute to the...
3
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"...
2
by: Jae | last post by:
Does anyone know of a way to rotate text in a datagrid? I have tried this : http://msdn.microsoft.com/workshop/samples/author/dhtml/refs/writingMode.htm but this technique did not work for me. ...
3
by: Harry | last post by:
Hi, Can anyone tell me why the below code does not work. I dont get any errors, but nor does the Text for the asp:label get set. Thanks in advance as always H
6
by: Paul | last post by:
I am trying to use a DataList and the ItemTemplate. I am binding the Datalist to a SQL query that gives me a list of Items with a Parent Category. I want to loop through all the items, but...
1
by: Jameel | last post by:
Hi Coders, How do i Add a Lable control to DataList to show the Count Files which belong the current Category. i have a DataList which displays categoryName,Description and File counter...
1
by: | last post by:
I have a label control that I've embedded in a datalist template. I will be binding data to that label. I want to run a string formatting function on the database text before it is injected into...
1
by: Steve Hershoff | last post by:
I'm using VS 2003, and need to extract the text from an asp label in my code-behind file. The label is part of a DataList's ItemTemplate. I've noticed that in my ascx file (we're using User...
0
by: rn5a | last post by:
One of the columns in a SQL Server 2005 DB table is named 'OrderDate' which stores the date & time on which an order has been placed. Since the 'OrderDate' of a particular order will be the same, I...
6
by: etam | last post by:
Hi, i have a DataList with a TextBox added by me: <asp:DataList ID="GradeDataList" runat="server" DataKeyField="id" DataSourceID="ProjectsObjectDataSource"> <ItemTemplate> topic: <asp:Label...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.