将嵌套重复器与存储过程结合使用

发布于 2024-09-30 02:44:04 字数 1677 浏览 0 评论 0原文

嘿,我想在 ASP.NET 2 中使用嵌套存储过程。

第一个存储过程返回所有营销活动,第二个存储过程返回营销活动中的所有项目。

我已经设置了 2 个中继器,现在我试图将参数从父中继器传递到子中继器存储过程,即活动 ID....这证明很棘手

在后面的代码中我想尝试

public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
    {
        SqlDataSource2.SelectParameters["campaignId"].DefaultValue =
            DataBinder.Eval(e.Item.DataItem, "campaignId").ToString();
    }

}

但我不知道如何调用此方法或加载它,如果我尝试此操作

<asp:Repeater ID="Repeater1" runat="server"  DataSourceID="SqlDataSource1" OnDataBinding="Repeater1_ItemDataBound">

,我收到错误

CS0123: No Heavy for 'Repeater1_ItemDataBound' matches delegate 'System.EventHandler'

任何帮助将不胜感激

编辑:将我的代码更改为

public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
    {
        Response.Write(DataBinder.Eval(e.Item.DataItem, "campaignId").ToString());
        SqlDataSource2.SelectParameters["campaignId"].DefaultValue =
            DataBinder.Eval(e.Item.DataItem, "campaignId").ToString();
        SqlDataSource2.SelectParameters["statusId"].DefaultValue =
            "1";

    }

    foreach (RepeaterItem repeaterItem in Repeater1.Items)
    {
        ((Repeater)(repeaterItem.FindControl("Repeater2"))).DataBind();

    }

}

但没有任何喜悦它传递将活动 ID 正确存储到存储过程中,但这在前端没有正确显示,

有什么想法吗?

Hey I would like to use Nested Stored Procedures in ASP.NET 2.

The first stored procedure returns all Campains and second one returns all items in the campaigns.

I have my 2 repeaters set up, and now I am trying to pass a parameter from the parent repeater to child repeater stored procedure i.e campaign id....this proving tricky

In the code behind I wanted to try

public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
    {
        SqlDataSource2.SelectParameters["campaignId"].DefaultValue =
            DataBinder.Eval(e.Item.DataItem, "campaignId").ToString();
    }

}

But I dont know how to call this method or get it to load if I try this

<asp:Repeater ID="Repeater1" runat="server"  DataSourceID="SqlDataSource1" OnDataBinding="Repeater1_ItemDataBound">

I get the error

CS0123: No overload for 'Repeater1_ItemDataBound' matches delegate 'System.EventHandler'

Any help would be greatly appreciated

EDIT : Changed mY Code Behind to

public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
    {
        Response.Write(DataBinder.Eval(e.Item.DataItem, "campaignId").ToString());
        SqlDataSource2.SelectParameters["campaignId"].DefaultValue =
            DataBinder.Eval(e.Item.DataItem, "campaignId").ToString();
        SqlDataSource2.SelectParameters["statusId"].DefaultValue =
            "1";

    }

    foreach (RepeaterItem repeaterItem in Repeater1.Items)
    {
        ((Repeater)(repeaterItem.FindControl("Repeater2"))).DataBind();

    }

}

But no joy its passing the correct campaign id to the stored procedure but this isnt displayed correctly on front end

any ideas ?

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

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

发布评论

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

评论(2

娇纵 2024-10-07 02:44:05

Microsoft 发布了使用嵌套转发器控件显示分层数据的分步指南:显示分层数据

请记住,可以从存储过程返回两个数据集(以两个 SELECT 结尾的存储过程)

这是使用 EnterpriseLibrary 的示例

    Try
        Using cmd As DbCommand = db.GetStoredProcCommand("spYourStoreProcedure")
            db.AddInParameter(cmd, "@customer", DbType.Int32, nCustomer)
            Using ds = db.ExecuteDataSet(cmd)
                ds.Tables(0).TableName = "Parent"
                ds.Tables(1).TableName = "Child"
                ds.Relations.Add("MyRelation", ds.Tables("Parent").Columns("customer"), ds.Tables("Child").Columns("customer"))

                ParentRepeater.DataSource = ds.Tables("Parent")
                ParentRepeater.DataBind()
            End Using
        End Using
    Catch ex As Exception
        ' Manage your excepion
        Exit Sub
    End Try

这是一个简单的嵌套转发器:

    <asp:Repeater ID="ParentRepeater" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <b><%# Container.DataItem("customer_name")%></b>
            <asp:Repeater ID="repFuncionesXArea" runat="server" DataSource='<%# Container.DataItem.Row.GetChildRows("MyRelation") %>' >
                <HeaderTemplate>
                   <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li><%# Container.DataItem("customer_history")%></li>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

Microsoft publish an step by step guide to display hierarchical data using Nested Repeater Controls: Display Hierarchical Data

Remember that it is posible to return two datasets from a Store Procedure (An store procedure that ends with two SELECTs)

This is an example using EnterpriseLibrary

    Try
        Using cmd As DbCommand = db.GetStoredProcCommand("spYourStoreProcedure")
            db.AddInParameter(cmd, "@customer", DbType.Int32, nCustomer)
            Using ds = db.ExecuteDataSet(cmd)
                ds.Tables(0).TableName = "Parent"
                ds.Tables(1).TableName = "Child"
                ds.Relations.Add("MyRelation", ds.Tables("Parent").Columns("customer"), ds.Tables("Child").Columns("customer"))

                ParentRepeater.DataSource = ds.Tables("Parent")
                ParentRepeater.DataBind()
            End Using
        End Using
    Catch ex As Exception
        ' Manage your excepion
        Exit Sub
    End Try

This is a simple nested repeater:

    <asp:Repeater ID="ParentRepeater" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <b><%# Container.DataItem("customer_name")%></b>
            <asp:Repeater ID="repFuncionesXArea" runat="server" DataSource='<%# Container.DataItem.Row.GetChildRows("MyRelation") %>' >
                <HeaderTemplate>
                   <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li><%# Container.DataItem("customer_history")%></li>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>
空袭的梦i 2024-10-07 02:44:04

您不应尝试附加 DataBinding 事件,而应附加 ItemDataBound 事件:

<asp:Repeater ID="Repeater1" runat="server" 
        DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">

DataBinding 事件适用于整个中继器,>ItemDataBound 将针对每个项目触发。

Instead of trying to attach the DataBinding event, you should be attaching the ItemDataBound event:

<asp:Repeater ID="Repeater1" runat="server" 
        DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">

The DataBinding event is for the whole repeater, the ItemDataBound will fire per item.

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