?
Solved

How to refer to label control inside itemtemplate

Posted on 2010-11-20
7
Medium Priority
?
537 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
[X]
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
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 332 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 332 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 336 total points
ID: 34217569
dim lblDone as Label = Label2
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses

762 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