Solved

How to refer to label control inside itemtemplate

Posted on 2010-11-20
7
507 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
Comment Utility
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
Comment Utility
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
Comment Utility
--------->  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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:zachvaldez
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
dim lblDone as Label = Label2
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now