Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to refer to label control inside itemtemplate

Posted on 2010-11-20
7
518 Views
Last Modified: 2012-06-27
I have the following mark up and code(I have problem in teh code side)

<asp:ListView ID="ListView1" DataSourceID="SqlDataSource1" Visible="false" DataKeyNames="UnitID" ConvertEmptyStringToNull="true"  runat="server" >
       
            <LayoutTemplate>
                <table cellpadding="2" runat="server" id="tblExcel" width="640px" cellspacing="0">
                    <tr runat="server" id="itemPlaceholder" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr id="Tr1" runat="server">
                    <%--<td>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>--%>
                    <td>
                        <asp:Label ID="lblUnitID" runat="server" Text='<%#Eval("UnitID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="ParishLabel" runat="server" Text='<%# Eval("School") %>' />
                     
                    </td>
                    <td>

                   
                    <asp:Button ID="SelectButton" runat="server" Text="Select"
                     CommandName="Select"
                     CommandArgument='<%#Eval("School")%>' />
                                       
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                                       
                    </td>

                </tr>
            </ItemTemplate>

 IN code behind I do to refer to it but getting Object not set...

 Dim lblDone As Label = FindControl("Label2")
            lblDone.Text = "Done"


I d like to see ways to access the label.

thnks
0
Comment
Question by:zachvaldez
7 Comments
 
LVL 12

Expert Comment

by:ErezMor
ID: 34181276
findcontrol as you used, looks under the main form, since your label is a child of the listview, you need to write ListView1.FindControl....
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34181309
Actually that won't work either. The ListView can display a series of records, so you need to use FindControl on a specific item. For example, it you wanted to get the label in the first row of the ListView you would use:

Dim lblDone As Label = CType(ListView1.Items(0).FindControl("Label2"), Label)

Open in new window

0
 
LVL 21

Assisted Solution

by:Alfred1
Alfred1 earned 83 total points
ID: 34181501
--------->  I d like to see ways to access the label.

You can also try using a FindControlRecursive function if you want.  See the function code below.  You can then call the function something like this:

Dim lblDone As Label = CType(FindControlRecursive(ListView1,"Label2"), Label)
lblDone.Text = "Done"
Private Function FindControlRecursive(ByVal ctrl As Control, ByVal id As String) As Control
        If ctrl.ID = id Then
            Return ctrl
        End If

        For Each ctrlChild As Control In ctrl.Controls
            Dim ctrlRecursive = FindControlRecursive(ctrlChild, id)
            If Not ctrlRecursive Is Nothing Then
                Return ctrlRecursive
            End If
        Next

        Return Nothing
    End Function

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:zachvaldez
ID: 34189001
Either way, code works but does not display the Label "DONE" in the row I assinged for <TD> </TD>

BTW, there are several rows, How would I reference which row or index Im clicking.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34189939
Have you got your ListView sat inside an AJAX updatepanel or anything like that?
0
 
LVL 3

Assisted Solution

by:Sokrates
Sokrates earned 83 total points
ID: 34195867
Try this

Since you are using  CommandName="Select", on the Command event you should be able to get the row/item from the ListView property SelectedIndex something like this:

((Label)ListView1.Rows[ListView1.SelectedIndex].FindControl("Label2")).Text = "DONE";
or
((Label)ListView1.Items[ListView1.SelectedIndex].FindControl("Label2")).Text = "DONE";

0
 
LVL 21

Accepted Solution

by:
Alpesh Patel earned 84 total points
ID: 34217569
dim lblDone as Label = Label2
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question