Solved

How to refer to label control inside itemtemplate

Posted on 2010-11-20
7
519 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 33
Echo'd values in dropdowns 6 27
Hta File displays dynamic File names 4 43
"lblTime is not declared" 3 28
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

685 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