Search code examples
vb.netdatasourcedatalistfindcontrol

Nested DataSource is not found


I have a datasource that needs to gather information for a label. These are both inside of a DataList that is connected to a different DataSource. When I debug my application, the value is Nothing. I thought I had the verbage right since there are no squiggly lines, but it isn't working. Can someone help me find the datasource so I can get this project done?

I tried FindControl("dsPicklist") and DirectCast(FindControl("dsPicklist"), SqlDataSource) but neither one gets a value returned.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Find the nested DataSource control in the DataList.
    Dim ds As SqlDataSource = DirectCast(FindControl("dsPicklist"), SqlDataSource)
    'convert the DataSource into a dataView
    Dim dv As DataView = DirectCast(ds.[Select](DataSourceSelectArguments.Empty), DataView)
    For Each drv As DataRowView In dv
        'Find the label
        Dim lbl As Label = FindControl("Label3")
        'Display the data into the label
        lbl.Text = dv("TEXT").ToString
    Next
End Sub

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
Width="100%" CellPadding="4" ForeColor="#333333">
<ItemTemplate>
<asp:HiddenField ID="hiddenPicklistID" runat="server"  
 Value='<%# Bind("PicklistID") %>' />
<asp:Label ID="Label3" runat="server"></asp:Label> 
    <asp:SqlDataSource ID="dsPicklist" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    SelectCommand="SELECT p.TEXT FROM PICKLIST p 
                   JOIN C_Survey_Questions c 
                   ON p.PICKLISTID = c.PicklistID 
                   AND c.QuestionID = @QuestionID 
                   AND c.SurveyID = @SurveyID 
                   WHERE p.PICKLISTID IS NOT NULL 
                   AND c.PicklistID IS NOT NULL">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" 
        PropertyName="SelectedValue" Type="Int32" />
        <asp:ControlParameter ControlID="HiddenField2" Name="QuestionID" 
        PropertyName="Value" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
</ItemTemplate>
</asp:DataList>

Solution

  • Although I dont know why you have the SqlDataSource inside the DataList, you have some error in the code. You should add <ItemTemplate> to your markup.

    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
      Width="100%" CellPadding="4" ForeColor="#333333">
      <ItemTemplate>
         <asp:HiddenField ID="hiddenPicklistID" runat="server"  
            Value='<%# Bind("PicklistID") %>' />
         <asp:Label ID="Label3" runat="server"></asp:Label> 
         <asp:SqlDataSource ID="dsPicklist" runat="server" 
             ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
             SelectCommand="SELECT p.TEXT FROM PICKLIST p 
                          JOIN C_Survey_Questions c 
                          ON p.PICKLISTID = c.PicklistID 
                          AND c.QuestionID = @QuestionID 
                          AND c.SurveyID = @SurveyID 
                          WHERE p.PICKLISTID IS NOT NULL 
                          AND c.PicklistID IS NOT NULL">
              <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" 
                  PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="HiddenField2" Name="QuestionID" 
                  PropertyName="Value" Type="Int32" />
              </SelectParameters>
         </asp:SqlDataSource>
      </ItemTemplate>  
    </asp:DataList>
    

    And in your code you find the SqlDataSource control inside the DataList1.Items

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Make sure DataList1 is databound before doing this
    
        For Each item As DataListItem In DataList1.Items
            Dim lbl As Label = DirectCast(item.FindControl("Label3"), Label)
            Dim ds As SqlDataSource = DirectCast(item.FindControl("dsPicklist"), SqlDataSource)
        Next
    End Sub