locked
Unable to access controls in nested listview RRS feed

  • Question

  • I have a listview control that functions the way I want it to.


    - a button that will toggle the visibility of  a table row.
    - clicking on the header sorts the listview by that column.

    each column is defined like the following
    '------------------------------------------------------------------------------------
     <th id="Th3" runat="server">
         <asp:LinkButton runat="server" ID="sortJobName"
         Text="Job Name" CommandName="Sort" CommandArgument="Job_Name" cssClass="button" Width="200px"/>
     </th>
    '------------------------------------------------------------------------------------
     
    the button is defined this way

    '------------------------------------------------------------------------------------
     <td>
         <asp:Button ID="Hide1" runat="server" CommandArgument='<%# Eval("JobID")%>' CommandName="Hide" Text="+"  />
         <asp:Label ID="HistoryDateLabel" runat="server" Text='<%# Format(Eval("History_Date"), "Short Date")%>' />
     </td>
    '------------------------------------------------------------------------------------
     
    I nested this control in another listveiw and changed the datasourse to a dataset that is created in the Page_Load event.

    '------------------------------------------------------------------------------------
     Dim dsOpen As New DataSet()
     Dim dataOpen As SqlDataAdapter = New SqlDataAdapter
     Dim strConn As String = ""
            Dim strSQL As String = "exec prcJob;"
            Dim sqlConn As SqlConnection

     sqlConn = New SqlConnection(strConn)
     dataOpen = New SqlDataAdapter(strSQL, sqlConn)
     dataOpen.TableMappings.Add("Category", "Category")
     dataOpen.TableMappings.Add("Category1", "JobTracking")

     dataOpen.Fill(dsOpen, "Category")
     dsOpen.Relations.Add("CatRel", dsOpen.Tables("Category").Columns("Category"), dsOpen.Tables("JobTracking").Columns("Category"))
     dsOpen.Relations(0).Nested = True

     Dim myListView As ListView
     myListView = FindControl("OpenJobs")
     myListView.DataSource = dsOpen.Tables("Category")
    '------------------------------------------------------------------------------------

    The new page displayes the Category Name in the outer listview and the jobs associated with the category in the inner
    listview. At this point neither the sort funtion or the button click work anymore. so I added code to add an event handler
    in the ItemDataBound event of the Categories listview.

    '------------------------------------------------------------------------------------
            Dim LV As ListView = CType(e.Item.FindControl("Jobs"), ListView)
            AddHandler LV.ItemCommand, AddressOf OpenJobs_ItemCommand
    '------------------------------------------------------------------------------------
           
    The handler triggers correctly because i see it executed for each category in the outer listveiw. And when I click the button, I see it step through OpenJobs_ItemCommand sub. However I don't seem to be able to change the visibility of the datarow.
    I read somewhere that I have to change the underlying data for it to show up in the listview. So I added a column to
    the dataset to control the visibility. The default is set to 0 or visible='False'.

    '------------------------------------------------------------------------------------
     visible='<%# Eval("ShowHistory")%>'
    '------------------------------------------------------------------------------------

    I can toggel this successfully in the Page_Load sub by setting a specific row to 1

    '------------------------------------------------------------------------------------
     dsOpen.Tables("JobTracking").Rows(4).Item("ShowHistory") = 1
    '------------------------------------------------------------------------------------
     
    But I can't seem to be able to do this in the ItemCommand sub. A trace indicates that it is finding the correct row and
    the value in the dataset is being set to 1.

    '------------------------------------------------------------------------------------
        Sub OpenJobs_ItemCommand(ByVal sender As ListView, ByVal e As ListViewCommandEventArgs)

            Dim myListView As ListView = sender

            If e.CommandName = "Hide" Then

                Dim txtSearch As String = "RequestId=" + e.CommandArgument.ToString
               
                For Each row In dsOpen.Tables("JobTracking").Select(txtSearch)

                    row.Item("ShowHistory") = 1
                    row.AcceptChanges()

                Next
         myListView.DataBind()
            End If

        End Sub
    '------------------------------------------------------------------------------------   

    I've tried a number of things including a databind of the underlying dataset. So far nothing has worked.

    The sorting function broke too. If I try to use the defaults, I get an error: The ListView 'JOBS' raised event Sorting which wasn't handled.
    After I add a handler like the ItemCommand Handler, I get a stack overflow exception.

    '------------------------------------------------------------------------------------   
        AddHandler LV.Sorting, AddressOf OpenJobs_Sorting

    '------------------------------------------------------------------------------------   
        Sub OpenJobs_Sorting(ByVal sender As ListView, ByVal e As ListViewSortEventArgs)
            sender.Sort(e.SortExpression, e.SortDirection)

        End Sub
    '------------------------------------------------------------------------------------   

    I didn't copy the entire code because it is quite large. I hope thee is enough here that someone might be able to see what I'm doing wrong.


    Jo Lee

    Wednesday, April 20, 2016 8:44 PM

All replies

  • Thanks for the detailed post Jo, but I'm afraid this isn't the correct forum for ASP.Net WebForms applications.  Since your issue is specific to web control databinding you'll likely get better responses from the ASP.Net forums.

    Please repost this at https://forums.asp.net


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Frank L. Smith Wednesday, April 20, 2016 10:48 PM
    Wednesday, April 20, 2016 9:36 PM
  • You are doing too much on the server side code addressing HTML controls instead of addressing the HTML controls on the client side using JavaScript and Jquery. 
    Thursday, April 21, 2016 2:32 AM
  • Thank you. I will repost in the correct forum.

    Jo Lee

    Thursday, April 21, 2016 2:14 PM
  • Thank you. I hadn't intended to do this much on the server side. I don't have a lot of experience doing this. So when I had a page working with very little custom code, I figured it wouldn't be too hard to nest this in another control. A week later I'm willing to agree that I was wrong. 

    Jo Lee

    Thursday, April 21, 2016 2:20 PM
  • Thank you. I hadn't intended to do this much on the server side. I don't have a lot of experience doing this. So when I had a page working with very little custom code, I figured it wouldn't be too hard to nest this in another control. A week later I'm willing to agree that I was wrong. 

    Jo Lee


    JavaScript is king and Jquery is right behind it in working with HTML controls. You should work with the HTML controls as much as possible on the client and not the server side.
    Thursday, April 21, 2016 10:58 PM