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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CharlieChilds
    New Member
    • Apr 2010
    • 9

    #1

    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:-
    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
    I thought that because “ DataList1.ItemT emplates.ItemTe mplate.Label2 - System.Web.UI.W ebControls.Labe l “ - 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.Item Templates" and if I hover over the squiggle the error box comes up saying " 'ItemTemplates' is not a member of 'System.Web.UI. WeControls.Data List'

    Thanks for any help on offer
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    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
    Code:
    <%@ Page Language="VB" EnableEventValidation="false" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head id="Head1" runat="server">
            <title></title>
        </head>
        <body>
            <form id="form1" runat="server">
                <div>
                    Selected Name: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  Age: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    
                    <asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" GridLines="Both">
                        <Columns>
                            <asp:BoundColumn DataField="KeyId" Visible="false"></asp:BoundColumn>
                            <asp:BoundColumn DataField="FullName" Visible="true"></asp:BoundColumn>
                            <asp:BoundColumn DataField="Age" Visible="true"></asp:BoundColumn>
                            <asp:ButtonColumn CommandName="Copy" DataTextField="SelectButton"></asp:ButtonColumn>
                        </Columns>
                    </asp:DataGrid>                
                </div>
            </form>
        </body>
    </html>

    Default.aspx.vb
    Code:
    Imports System.Data
    
    Partial Class Default
        Inherits System.Web.UI.Page
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            FillDataList()
        End Sub
    
        Sub FillDataList()
            Dim mydatatable As New DataTable
            mydatatable.Columns.Add("KeyId", Type.GetType("System.String"))
            mydatatable.Columns.Add("FullName", Type.GetType("System.String"))
            mydatatable.Columns.Add("Age", Type.GetType("System.String"))
            mydatatable.Columns.Add("SelectButton", Type.GetType("System.String"))
    
            Dim myrow As DataRow
    
            myrow = mydatatable.NewRow
            myrow("KeyId") = 1
            myrow("FullName") = "Izabella Miko"
            myrow("Age") = 29
            myrow("SelectButton") = "<input id='Button1' type='button' value='Select' onclick=""__doPostBack('DataGrid1$ctl02$ctl00')"" />"
            mydatatable.Rows.Add(myrow)
    
            myrow = mydatatable.NewRow
            myrow("KeyId") = 2
            myrow("FullName") = "Tiffani-Amber Thiessen"
            myrow("Age") = 36
            myrow("SelectButton") = "<input id='Button1' type='button' value='Select' onclick=""__doPostBack('DataGrid1$ctl03$ctl00')"" />"
            mydatatable.Rows.Add(myrow)
    
            myrow = mydatatable.NewRow
            myrow("KeyId") = 3
            myrow("FullName") = "Mischa Barton"
            myrow("Age") = 24
            myrow("SelectButton") = "<input id='Button1' type='button' value='Select' onclick=""__doPostBack('DataGrid1$ctl04$ctl00')"" />"
            mydatatable.Rows.Add(myrow)
    
    
            DataGrid1.DataSource = mydatatable
            DataGrid1.DataBind()
        End Sub
    
        Protected Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
            Select Case (e.CommandName)
                Case "Copy"
                    TextBox1.Text = e.Item.Cells(1).Text.ToString
                    TextBox2.Text = e.Item.Cells(2).Text.ToString
                Case Else
                    ' You can add more cases for other functions.
            End Select
        End Sub
    End Class
    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~

    Comment

    • CharlieChilds
      New Member
      • Apr 2010
      • 9

      #3
      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.
      Code:
          protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
          {  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 + " " + label2.Text + Environment.NewLine;
              }
          }
      It works a treat.
      Thanks again for your effort and advice. I'll be looking up DataGrid
      Last edited by Frinavale; Apr 27 '10, 08:09 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      Working...