找不到嵌套的DataSource

时间:2012-02-08 22:27:33

标签: vb.net datasource datalist findcontrol

我有一个需要收集标签信息的数据源。它们都在连接到不同DataSource的DataList内部。当我调试我的应用程序时,值为Nothing。我以为我有正确的问题,因为没有波浪线,但它不起作用。有人可以帮我找到数据源,这样我可以完成这个项目吗?

我尝试了FindControl("dsPicklist")DirectCast(FindControl("dsPicklist"), SqlDataSource),但没有人获得返回值。

    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>

1 个答案:

答案 0 :(得分:0)

虽然我不知道为什么SqlDataSource中有DataList,但代码中有一些错误。您应该将<ItemTemplate>添加到标记中。

<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>

在您的代码中,您可以在SqlDataSource

中找到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