首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中继器排序ItemDataBound

中继器排序ItemDataBound
EN

Stack Overflow用户
提问于 2017-06-30 22:21:19
回答 1查看 306关注 0票数 0

我在ASP中继器和尝试对数据进行排序时遇到了一些问题。

因此,在Page_Load上,我获得的数据源如下...

代码语言:javascript
复制
    Protected Overloads Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not (Page.IsPostBack) Then

            'No need to re-load Occasion because it's done in the PreLoad event
            Me.Header.Title = "OCP - " + Occasion.Checklist.name

            pnlStatistics.Visible = Not (Occasion.isClosed)
            pnlClose.Visible = SecurityHelper.HasRole(SecurityMatchOption.AtLeast, SecurityRole.Manager, _relevantTeam) And Not (Occasion.isClosed)

            'initially assume checklist can be closed. any un-signed off task in the item_databound event will disable it.
            btnClose.Enabled = True

            'Fix Issue 63: rptTask.DataSource = _db.vw_tasklists.Where(Function(o) o.occasionId = Occasion.id).ToList()
            rptTask.DataSource = _db.vw_tasklists.Where(Function(o) o.occasionId = Occasion.id).OrderBy(Function(t) t.taskDueDate).ThenBy(Function(t) t.taskDueTime)

但是,在ItemDataBound中,我们重新计算任务duedate。

代码语言:javascript
复制
    Private Sub rptTask_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptTask.ItemDataBound

        ' Get the data relating to this task 'row'
        Dim t = CType(e.Item.DataItem, vw_tasklist)

        If e.Item.ItemType = ListItemType.Header Then
            Dim thDueDate = CType(e.Item.FindControl("thDueDate"), HtmlTableCell)

            thDueDate.Visible = Not (Occasion.isClosed)
        End If

        'securable buttons

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            ' Dynamically create a span element named TASKnnnn, which will be referenced from
            ' 'child page' links back to this page in order to vertically reposition at the selected task
            Dim span = New HtmlGenericControl("span")
            span.ID = "TASK" & t.taskId
            span.ClientIDMode = ClientIDMode.Static                             ' prevent ASP.NET element ID mangling
            e.Item.FindControl("lblTaskName").Parent.Controls.AddAt(0, span)    ' hook it into the repeater row

            Dim btnSignoff = CType(e.Item.FindControl("btnSignOff"), Button)
            Dim btnComment = CType(e.Item.FindControl("btnComment"), Button)
            Dim btnAmend = CType(e.Item.FindControl("btnAmend"), Button)
            Dim btnView = CType(e.Item.FindControl("btnView"), Button)
            Dim lblSoTime = CType(e.Item.FindControl("lblSOTime"), Label)
            Dim lblDueDate = CType(e.Item.FindControl("lblDueDate"), Label)
            Dim lblTaskId = CType(e.Item.FindControl("lblTaskId"), Label)
            Dim lblTaskName = CType(e.Item.FindControl("lblTaskName"), Label)

            lblTaskId.Text = CType(t.taskId, String)

            lblTaskName.Text = t.taskName

            Dim time = (If(t.taskDueTime Is Nothing, New TimeSpan(0, 23, 59, 59), TimeSpan.Parse(t.taskDueTime)))
            Dim dueDateTime As DateTime = (Occasion.started.Date + time)

            'Setup up due DateTime for Daily Tasks
            Select Case DirectCast(t.taskDayTypeId, helpers.Constants.enDayType)
                Case helpers.Constants.enDayType.Daily
                    lblDueDate.Text = dueDateTime.ToString("dd/MM/yyyy HH:mm")
                    Exit Select
                Case Else
                    'Calculate the actual due date for non-daily tasks
                    Dim calculator = New Calculator()
                    Dim calId = t.taskCalendarId
                    Dim taskMonthDay = "1"
                    If Not t.taskMonthDayId Is Nothing Then
                        taskMonthDay = CType(t.taskMonthDayId, String)
                    End If
                    Dim monthDay = _db.MonthDays.First(Function(m) m.id = CInt(taskMonthDay))
                    Dim calendar As Model.Calendar = Nothing
                    If Not calId is Nothing Then
                        calendar = _db.Calendars.First(Function(x) calId.Value = x.id)
                    End If
                    Dim potDate = calculator.GetActualDueDate(dueDateTime, monthDay.monthDay, t, calendar)
                    dueDateTime = (potDate.Date + time)
                    lblDueDate.Text = dueDateTime.ToString("dd/MM/yyyy HH:mm")
                    Exit Select
            End Select

因此,一旦数据在中继器中显示,排序就会出错。我需要能够在到期日期重新计算后对数据进行排序。这怎麽可能?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-06-30 22:44:56

您可以将ItemDataBound逻辑上移到原始查询中:

代码语言:javascript
复制
rptTask.DataSource = _db.vw_tasklists.
    Where(Function(o) o.occasionId = Occasion.id).
    Select(
          Function(r)
              'Fill in the logic here so the DueProperty has your real Due Date
              New With {.Row = r, .DueDate = r.TaskDueDate + r.TaskDueTime}
           End Function
    OrderBy(Function(t) t.DueDate). ' Now we only need one OrderBy (the time is already included).
    Select(Function(r) r.Row) 'But we do want to select back to the original record for the databinding
    'And the ToList() was probably NEVER needed or helpful in this situation

此外,由于这看起来像linq- to -sql,我可能会将其分解,因此我们将期望在数据库上执行的内容与期望在web服务器上执行的内容完全分开:

代码语言:javascript
复制
'This runs on the databsae
Dim sqlData = _db.vw_tasklists.
    Where(Function(o) o.occasionId = Occasion.id)

'This runs on the web server
rptTask.DataSource = sqlData.
    Select(
          Function(r)
              'Fill in the logic here so the DueProperty has your real Due Date
              New With {.Row = r, .DueDate = r.TaskDueDate + r.TaskDueTime}
           End Function
    OrderBy(Function(t) t.DueDate). ' Now we only need one OrderBy (the time is already included).
    Select(Function(r) r.Row) 'But we do want to select back to the original record for the databinding
    'And the ToList() was probably NEVER needed or helpful in this situation

当然,如果您开始将信息放入数据库中,以便在一开始就能有效地对数据进行正确排序,那么您将获得最佳结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44848707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档