0

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>
Jamie
  • 1,579
  • 8
  • 34
  • 74

1 Answers1

0

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
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • Whoops, sorry. `ItemTemplate` is there. I just copied and pasted parts of the page because the other labels aren't relevant to this question. I put the datasource inside the datalist so that it was easier for the hiddenfield and datasource to find each other. I have tried putting an extra datasource outside of a list before and it had problems with the hiddenfield. – Jamie Feb 08 '12 at 22:52
  • I can't get anything to show up. The code that you wrote seems to be just what I need but I guess I just am not databinding right? At first I just wrote `DataList1.DataBind()` but nothing appeared in the label. Since then I have re-written it plenty of times, and each time I get no results. The results of the query show up just fine in a bulleted list because their are so many options, but I can't use a bulleted list because a user needs to be able to click on yes or no because this is for a survey. – Jamie Feb 09 '12 at 17:33
  • I think you need another nested DataList inside the first that has the label you want to repeat. And than set the datasource of that datalist to dsPicklist. And you wont need any codebehind. – Magnus Feb 09 '12 at 17:48
  • Oh I wasn't even aware you could do that. :) – Jamie Feb 09 '12 at 19:08
  • I nested a DataList and was able to pull the information that I need! Thanks so much! – Jamie Feb 09 '12 at 19:46