【问题标题】:SqlDataSource reuse on different pagesSqlDataSource 在不同页面上的重用
【发布时间】:2012-09-22 06:50:06
【问题描述】:

我正在使用 ASP.NET。我有一个 ReportPage1 和 ReportOutputPage1。这些是不同的 aspx 文件,并且具有不同的 MasterPages。但是,我需要在两个页面上使用相同的 SqlDataSource 对象。在 ReportPage 上我需要 SqlDataSource 来调用 StoredProcedure 并将数据导入 CSV 文件,但在 ReportOutputPage 上我需要使用 SqlDataSource 来调用相同的 StoredProcedure 并填充 GridView。

ReportPage1 是“主”页面 - 从该页面单击按钮会打开 ReportOutputPage1 并将其显示在新窗口中。 ReportPage 是 ReportOutputPage1 的 PreviousPage

以上是 Report1 的示例。 Report2(带有 SqlDataSource2)和 Report3(SqlDataSource3)等的想法相同 - 10 种不同的报告。

如何对每两个页面(ReportPage & ReportOutputPage)复用SqlDataSource?

  1. 我在 Web 中找到的第一个建议 - 对 ReportPage 和 ReportOutputPage 都使用母版页。这不起作用,因为我已经为 ReportPage 和 ReportOutputPage 建立了不同的母版页,然后我需要为每个报告创建 10 个不同的母版页。

  2. 第二个建议是在 ReportPage 上定义 SqlDataSource,然后在 ReportOutputPage 上使用 PrevousePage 重用它,但这不适用于我的特殊情况(我正在使用 Ajax 人员和部分页面回发,而且我正在丢失 PreviousPage,也SqlDataSource 无法序列化以将其保存在 ViewState 或类似中)。

  3. 创建用户控件。可能这可行,但每次为新报告(10 个报告 - 10 个用户控件?)创建 UserControl 非常耗时。

  4. 简单地复制和粘贴 SqlDataSource(我为一份报告做了)可以工作,但我想要更像代码重用的东西。如有必要,有人可能会忘记在两个不同的地方修改 SqlDataSource。

能否请您给我一些建议,如何为报告和报告输出页面重用代码(特别是 SqlDataSource)?

【问题讨论】:

    标签: asp.net code-reuse


    【解决方案1】:

    您能否定义使用相同的SqlDataSource 的需求?如果它们是两个不同的页面并且听起来像是两种不同的用途,为什么不使用两个不同的SqlDataSource?无论如何,这些页面是分开的,您不能在一个页面上与另一个页面共享一个对象。

    我建议您考虑在应用程序中添加一个数据层以进行数据库交互,并在请求时将您的数据绑定到数据网格。这样,您只需构建一次数据库交互,就可以在不同的页面上重复使用它。

    另一种选择是您只需使用两个SqlDataSources 并将它们复制/粘贴到两个页面。如果您尝试进行选择或某种标准适用于您的第二页,请考虑在您的SqlDataSource 中使用查询字符串和QueryStringParameters。

    希望这会有所帮助。

    编辑:在 App_Code 的某个地方弹出这个,传入你的具体使用要求。

    Public Shared Function GetData(connString As String, command As String, Optional params As SqlParameter() = Nothing) As DataTable
        Dim conn As New SqlConnection(connString)
        Dim da As New SqlDataAdapter(command, conn)
        Dim dt As New DataTable
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.CommandTimeout = 6000 '6 seconds.
        If Not IsNothing(params) Then
            For Each p As SqlParameter In params
                da.SelectCommand.Parameters.Add(p)
            Next
        End If
        Try
            conn.Open()
            da.Fill(dt)
            Return dt
        Catch ex As Exception
            Throw ex
        Finally
            conn.Close()
        End Try
    End Function
    

    然后将数据表绑定到您的gridview,不确定您如何输出到文件。

    Private Sub BindData(gridview As GridView, data As DataTable)
        gridview.DataSource = data
    End Sub
    

    您现在可以从后面的代码中重用数据库交互:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
       BindData(MyGridView,GetData(ConnectionStrings(connName).ConnectionString, _
                     "dbo.SomeSprocOrOther", _
                     New SqlParameter(){New SqlParameter("paramName","paramValue")})
    End Sub
    

    【讨论】:

    • Report 和 ReportOutput 使用相同结构的相同 SqlDataSource 并调用相同的 StoredProcedure。报告不显示结果,但允许用户下载文件。但是,ReportOutput 在 GridView 中显示数据。因此,两者都使用具有相同参数的相同 SqlDataSource。请参阅使用复制/粘贴解决方案编辑的帖子。
    • 即使我将 GetReport1Data 移动到 DataLayer,我仍然需要在 Report 和 ReportOutput 页面上都有一些 ObjectDataSource。
    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多