2

I have a ListView called "orderReceiptTable" which I am able to properly access from the Code Behind. Within it is a literal called "orgName" which I obviously would like to populate with an organization's name.

After much searching it was determined that FindControl was the right course of action. Perhaps I am using FindControl improperly but I am unable to actually have it "find" my Literal control.

The code block is being called in the Page Load.

My code looks as such:

    Dim orgNameString As String = getOrganizationName.getOrgName(organizationID).ToString()

    Dim myOrgName As Literal = FindControl("orgName")
    myOrgName = CType(orderReceiptTable.FindControl("orgName"), Literal)

    If Not (myOrgName Is Nothing) Then
        Response.Write("I found the control!")
        myOrgName.Text = orgNameString
    End If

Here is the mark-up in the .aspx file:

<asp:ListView ID="orderReceiptTable" runat="server">
            <LayoutTemplate>
                <div runat="server" id="itemPlaceholder" />
            </LayoutTemplate>

            <EmptyDataTemplate>
                <tr id="noDataDiv" runat="server"> 
                    <td class="sub" ID="itemPlaceholder" runat="server">
                        No order data was returned.
                    </td>
                </tr> 
            </EmptyDataTemplate>

            <ItemTemplate>
                <div id="itemPlaceholder" runat="server" style="border:solid 1px #000000; width:250px; float:left; padding:10px; border:solid 2px #1664B1;">
                    <div>Organization Name: <asp:Literal runat="server" ID="orgName"></asp:Literal></div>                             
                </div>
            </ItemTemplate>        
    </asp:ListView>   
BehemothDan
  • 316
  • 3
  • 15
  • 2
    Can you include the HTML markup for the listview so that we can understand how it's setup? Also, can you indicate where the above code is being called? That could impact the answer. – competent_tech Jan 05 '12 at 01:28
  • 1
    Do you have a template in your `ListView`? If so, try `myOrgName = CType(orderReceiptTable.Controls(0).FindControl("orgName"), Literal)`. I've put `0` as the index asI assume only one level of template if there is one. – keyboardP Jan 05 '12 at 01:31
  • I added the mark-up to my question if that helps. I am working with your suggestion keyboardP to see if that works with some editing. I will let you know. – BehemothDan Jan 05 '12 at 16:26

1 Answers1

1

The controls inside the template will only be created after binding some data to it. You will then be able to access it via the ListView.Controls property.

This previous answer might help: Find control in ListView EmptyDataTemplate

Community
  • 1
  • 1
Richard
  • 1,122
  • 1
  • 7
  • 14