Retrieve datatable.column value in rpt_RowDataBound sub in asp.net

岁酱吖の 提交于 2019-12-11 06:22:57

问题


Based on the selected answer in this post Displaying records grouped by month and year in asp.net, I have a modification to work with.

In the rpt_RowDataBound sub, instead of using a FileInfo object, I need to use a database value ("statusupdate").

If month <> TryCast(e.Item.DataItem, FileInfo).CreationTime.Month OrElse year <> TryCast(e.Item.DataItem, FileInfo).CreationTime.Year Then

How can I replace the "FileInfo" in the above code line with datarow("statusupdate")

Code:

HTML:

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_RowDataBound" Visible="true">
    <ItemTemplate>
        <table width="100%" runat="server" visible="true" id="headerTable">
            <tr>
                <td colspan="3" style="color: White;" class="TextFont"><asp:Label ID="headerTitle" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td Font-Size="Smaller" align="center" style="width:10%; background-color: #3A4F63; color: White;">View<br>Record</td>
                <td Font-Size="Smaller" align="center" style="width:20%; background-color: #3A4F63; color: White;">ManagerID</td>
                <td Font-Size="Smaller" style="width:20%; background-color: #3A4F63; color: White;">DBCost</td>
                <td Font-Size="Smaller" style="width:20%; background-color: #3A4F63; color: White;">GroupID</td>
                <td Font-Size="Smaller" style="width:20%; background-color: #3A4F63; color: White;">Updated</td>
                <td Font-Size="Smaller" style="width:20%; background-color: #3A4F63; color: White;">RepID</td>
            </tr>
        </table>
        <table width="100%" border="1">
            <tr>
                <td style="width:10%;" align="center"><asp:Label ID="lblISONum" runat="server"></asp:Label>
                    <asp:HyperLink ID="imgFileType" ImageUrl="images/mag.gif" NavigateUrl='<%# SetNavigateUrl(Eval("RecID")) %>' runat="server"></asp:HyperLink>
                </td>
                <td style="width:15%;"><asp:Label Font-Size="Smaller" ID="lblMID" runat="server" Text='<%#Eval("managerid") %>'></asp:Label></td>
                <td style="width:20%;"><asp:Label Font-Size="Smaller" ID="lblDBA" runat="server" Text='<%#Eval("dbCost") %>'></asp:Label></td>
                <td style="width:20%;"><asp:Label Font-Size="Smaller" ID="lblGroup" runat="server" Text='<%#Eval("GroupID") %>'></asp:Label></td>
                <td style="width:20%;"><asp:Label Font-Size="Smaller" ID="lblStatus" runat="server" Text='<%#Eval("statusdate") %>'></asp:Label></td>
                <td style="width:20%;"><asp:Label Font-Size="Smaller" ID="lblSalesRep" runat="server" Text='<%#Eval("SalesRepID") %>'></asp:Label></td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

Code Behind:

Protected Sub rpt_RowDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        If month <> TryCast(e.Item.DataItem, FileInfo).CreationTime.Month OrElse year <> TryCast(e.Item.DataItem, FileInfo).CreationTime.Year Then
            month = TryCast(e.Item.DataItem, FileInfo).CreationTime.Month
            year = TryCast(e.Item.DataItem, FileInfo).CreationTime.Year
            e.Item.FindControl("headerTable").Visible = True
            TryCast(e.Item.FindControl("headerTitle"), Label).Text = MonthName(TryCast(e.Item.DataItem, FileInfo).CreationTime.Month()) & " " & TryCast(e.Item.DataItem, FileInfo).CreationTime.Year()
        Else
            e.Item.FindControl("headerTable").Visible = False
        End If
    End If
End Sub

Page_Load event:

        Dim dt As DataTable = New DataTable()
        Dim da As SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
        da.Fill(dt)
        rpt.DataSource = dt
        rpt.DataBind()

回答1:


Try this:

Protected Sub rpt_RowDataBound(sender As Object, e As RepeaterItemEventArgs)

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

        If month <> DateTime.Parse(TryCast(e.Item.DataItem, DataRowView)("statusupdate").ToString()).Month OrElse year <> DateTime.Parse(TryCast(e.Item.DataItem, DataRowView)("statusupdate").ToString()).Year Then
            month = DateTime.Parse(TryCast(e.Item.DataItem, DataRowView)("statusupdate").ToString()).Month
            year = DateTime.Parse(TryCast(e.Item.DataItem, DataRowView)("statusupdate").ToString()).Year
            e.Item.FindControl("headerTable").Visible = True
            TryCast(e.Item.FindControl("headerTitle"), Label).Text = "Files for " & DateTime.Parse(TryCast(e.Item.DataItem, DataRowView)("statusupdate").ToString()).ToShortDateString()
        Else
            e.Item.FindControl("headerTable").Visible = False
        End If
    End If
End Sub



回答2:


UPDATE

Actually, looking at this again, I think what you need to replace FileInfo with is DateTime, assuming again that "statusupdate" in the database is a DateTime column. In other words:

Protected Sub rpt_RowDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)  

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then                      
        If month <> TryCast(e.Item.DataItem, DateTime).Month OrElse year <> TryCast(e.Item.DataItem, DateTime).Year Then                          
            month = TryCast(e.Item.DataItem, DateTime).Month
            year = TryCast(e.Item.DataItem, DateTime).Year
            e.Item.FindControl("headerTable").Visible = True                         
            TryCast(e.Item.FindControl("headerTitle"), Label).Text = MonthName(TryCast(e.Item.DataItem, DateTime).Month) & " " & TryCast(e.Item.DataItem, DateTime).Year
        Else                          
            e.Item.FindControl("headerTable").Visible = False                      
        End If                  
    End If              
End Sub              

The example you had used a FileInfo object, and you're using (it appears) a DateTime object. So all you need to do is replace FileInfo with DateTime.



来源:https://stackoverflow.com/questions/7761280/retrieve-datatable-column-value-in-rpt-rowdatabound-sub-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!