使用存储过程参数的嵌套数据列表

发布于 2024-07-08 21:19:15 字数 4636 浏览 6 评论 0原文

我正在使用一些嵌套数据列表控件,但无法获取使我的嵌套存储过程正常工作的 SP 参数。

在调试中我可以看到 SqlDataSource2.SelectParameters.Add("Section",oLabel.Text.ToString()); 正在从标签中获取正确的值,但是当显示结果时,我总是得到最后添加的参数的结果?

我猜想每次绑定嵌套数据列表时我都需要以某种方式清除参数,但是如果我添加代码来执行此操作,则会导致我未指定参数的错误?

我的代码如下,你会看到最终我将有 3 个嵌套的数据列表 - 或者这就是计划。

感谢您的任何建议

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="nhscsharprepeater._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Data" %>

<script type="text/C#" runat="server">





    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        DataList oList = (DataList)e.Item.FindControl("Datalist2");
        Label oLabel = (Label)e.Item.FindControl("lblSection");

        DataList oList2 = (DataList)oList.FindControl("Datalist3");


        SqlDataSource2.SelectParameters.Clear();        
        SqlDataSource2.SelectCommand = "report_DistinctSubSections";
        SqlDataSource2.SelectParameters.Add("Section",oLabel.Text.ToString());



        oList.DataBind();

    }



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
            SelectCommand="report_DistinctSection" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
             SelectCommandType="StoredProcedure">

             <SelectParameters>
                <asp:Parameter Name="Section" Type="String" />
             </SelectParameters>

             </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
            SelectCommand="report_getReports" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter Name="SubSection" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
            <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="2" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound">
                   <ItemTemplate>

                            <asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Section") %>' ID="lblSection">
                            </asp:Label>

                            <br />

                            <asp:datalist id="Datalist2" runat="server" DataSourceID="SqlDataSource2">                          
                                    <ItemTemplate>

                                                <asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "SubSection") %>' ID="lblSection">
                                                </asp:Label>                                    

                                                <br />

                                                   <asp:datalist id="Datalist3" runat="server" DataSourceID="SqlDataSource3">                           
                                                            <ItemTemplate>

                                                                        <!--<asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Report") %>' ID="lblSection">
                                                                        </asp:Label>-->                                                                 

                                                            </ItemTemplate>
                                                    </asp:datalist>                                                


                                    </ItemTemplate>
                            </asp:datalist>



                </ItemTemplate>
           </asp:DataList> 
    </div>
    </form>
</body>
</html>

I'm working with some nested datalist controls and cannot get the SP parameters for my nested stored procedure to work.

In debug I can see that SqlDataSource2.SelectParameters.Add("Section",oLabel.Text.ToString()); is getting the correct value from the label but when the results display I always get the results for the last parameter added?

I'm guessing I need to clear the parameters in some way each time the nested datalist is bound but if I add code to do that it results in an error that I have not specified the parameters?

My code is below, you'll see that eventually I will have 3 nested datalists inside each other - or that's the plan.

Thanks for any suggestions

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="nhscsharprepeater._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Data" %>

<script type="text/C#" runat="server">





    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        DataList oList = (DataList)e.Item.FindControl("Datalist2");
        Label oLabel = (Label)e.Item.FindControl("lblSection");

        DataList oList2 = (DataList)oList.FindControl("Datalist3");


        SqlDataSource2.SelectParameters.Clear();        
        SqlDataSource2.SelectCommand = "report_DistinctSubSections";
        SqlDataSource2.SelectParameters.Add("Section",oLabel.Text.ToString());



        oList.DataBind();

    }



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
            SelectCommand="report_DistinctSection" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
             SelectCommandType="StoredProcedure">

             <SelectParameters>
                <asp:Parameter Name="Section" Type="String" />
             </SelectParameters>

             </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
            SelectCommand="report_getReports" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter Name="SubSection" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
            <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="2" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound">
                   <ItemTemplate>

                            <asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Section") %>' ID="lblSection">
                            </asp:Label>

                            <br />

                            <asp:datalist id="Datalist2" runat="server" DataSourceID="SqlDataSource2">                          
                                    <ItemTemplate>

                                                <asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "SubSection") %>' ID="lblSection">
                                                </asp:Label>                                    

                                                <br />

                                                   <asp:datalist id="Datalist3" runat="server" DataSourceID="SqlDataSource3">                           
                                                            <ItemTemplate>

                                                                        <!--<asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Report") %>' ID="lblSection">
                                                                        </asp:Label>-->                                                                 

                                                            </ItemTemplate>
                                                    </asp:datalist>                                                


                                    </ItemTemplate>
                            </asp:datalist>



                </ItemTemplate>
           </asp:DataList> 
    </div>
    </form>
</body>
</html>

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

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

发布评论

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

评论(1

落花浅忆 2024-07-15 21:19:15

有人可能会猜测调用 Clear 在调用 Add 之前调用 SqlDataSource2.SelectParameters 可能会成功。

One might guess that calling Clear on SqlDataSource2.SelectParameters before calling Add might do the trick.

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