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

时间:2022-02-03 10:57:17

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

我正在使用一些嵌套的datalist控件,无法获取嵌套存储过程的SP参数。

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?

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

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?

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

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

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

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>

1 个解决方案

#1


1  

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

有人可能会猜到在调用Add之前在SqlDataSource2.SelectParameters上调用Clear可能会有所作为。

#1


1  

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

有人可能会猜到在调用Add之前在SqlDataSource2.SelectParameters上调用Clear可能会有所作为。