By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,713 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,713 IT Pros & Developers. It's quick & easy.

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

P: 9
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
Share this Question
Share on Google+
2 Replies


CroCrew
Expert 100+
P: 564
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

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

Post your reply

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