Solved

How to refer to label control inside itemtemplate

Posted on 2010-11-20
7
510 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now