2012-02-08 67 views
0

我有一個需要收集標籤信息的數據源。這些都在連接到不同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> 

回答

0

雖然我不知道爲什麼你有DataListSqlDataSource,你必須在代碼中的一些錯誤。您應該將<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> 

並在代碼中找到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 
+0

哎呦內SqlDataSource控制,對不起。 'ItemTemplate'在那裏。我只是複製並粘貼頁面的某些部分,因爲其他標籤與此問題無關。我將數據源放在數據列表中,以便隱藏域和數據源更容易找到對方。我曾嘗試在列表之外添加額外的數據源,並且在隱藏域中存在問題。 – jlg 2012-02-08 22:52:30

+0

我無法得到任何東西顯示。你寫的代碼似乎只是我需要的,但我想我不是數據綁定的權利?起初我只寫了'DataList1.DataBind()',但沒有出現在標籤中。從那以後,我重新寫了很多次,每次都沒有結果。查詢結果在項目符號列表中顯示得很好,因爲它們有很多選項,但我不能使用項目符號列表,因爲用戶需要能夠點擊是或否,因爲這是用於調查。 – jlg 2012-02-09 17:33:59

+0

我想你需要另一個嵌套的DataList,第一個有你想要重複的標籤。並將該datalist的數據源設置爲dsPicklist。你不需要任何代碼隱藏。 – Magnus 2012-02-09 17:48:18