找不到嵌套数据源

发布于 2025-01-03 14:56:41 字数 2057 浏览 1 评论 0原文

我有一个数据源需要收集标签信息。它们都位于连接到不同数据源的 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2025-01-10 14:56:41

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

<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 内找到 SqlDataSource 控件

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文