在Gridview中检查过期日期

时间:2018-01-23 13:51:03

标签: asp.net vb.net

我对编程和调整一些已经为我编写的代码相当新。我想检查Gridview中的日期(名为:playDate)是否早于当前日期,如果是,则注册页面图标将其可见性设置为false。

但我收到了错误:

BC30311: Value of type 'System.Web.UI.Control' cannot be converted to 'Date'

错误似乎出现在这一行:

playDate = r.Cells(1).FindControl("playDate")

这是前端代码:

        <asp:SqlDataSource ID="DSCompetitions" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT     TOP (100) PERCENT tblCompetitions.compID, tblCompetitions.compName, tblCompetitions.playDate, tblCompetitions.venue, tblCompetitions.entryPrice, tblCompetitions.rules, tblCompetitions.maxPlayers
        FROM tblCompetitions                

        ORDER BY tblCompetitions.playDate DESC"></asp:SqlDataSource>
        <asp:Gridview ID="gdvCompetitions" width="100%"  runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="compID" DataSourceID="DSCompetitions" PageSize="20" AllowSorting="True">

            <AlternatingRowStyle CssClass="alt" />
            <Columns>

                <asp:BoundField DataField="compName" HeaderText="Event" />
                <asp:BoundField DataField="playDate" DataFormatString = "{0:dd/MM/yyyy}" HeaderText="Date" />
                <asp:BoundField DataField="venue" HeaderText="Venue" />
                <asp:BoundField DataField="entryPrice" HeaderText="Entry Fee £" />

                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:HyperLink ID="hypView" runat="server" NavigateUrl='<%# "~/competition-view.aspx?compID=" & Eval("compID").ToString & "&round=1" %>'><asp:Image ID="ImgView" Width="30px" runat="server" ImageUrl="~/files/images/icons/view-icon.png" /></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:HyperLink ID="hypBook" runat="server" NavigateUrl='<%# "~/competition-book.aspx?compID=" & Eval("compID").ToString %>'><asp:Image ID="ImgRegister" Width="30px" runat="server" ImageUrl="~/files/images/icons/register-icon.png" /></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>

这是我的VB代码:

For Each r As GridViewRow in gdvCompetitions.Rows
    If r.RowType = DataControlRowType.DataRow Then 'Execute the code only for datarow, excluding footer and header
        Dim playDate As Date
        playDate = r.Cells(1).FindControl("playDate") 
        Dim hypBook As Hyperlink
        hypBook = r.Cells(5).FindControl("hypBook") 
        If date.now > playDate Then 
            hypBook.visible=false
        End If
    End If
Next r

3 个答案:

答案 0 :(得分:0)

您将playDate定义为日期,然后使用playDate = r.Cells(1).FindControl("playDate")控件启动它,这是不正确的。

FindControl会找到一个控件,因为你知道我们不能用ASP.Net控件启动datetime变量所以 将您的代码更改为:

For Each r As GridViewRow in gdvCompetitions.Rows
    If r.RowType = DataControlRowType.DataRow Then 'Execute the code only for datarow, excluding footer and header
        Dim playDate As Date
        playDate = Convert.ToDateTime(r.Cells(1).Text)
        Dim hypBook As Hyperlink
        hypBook = r.Cells(5).FindControl("hypBook") 
        If date.now > playDate Then 
            hypBook.visible=false
        End If
    End If
Next r

我不是VB.Net程序员,但我确信我的解释是正确的,更改的代码也可以。

答案 1 :(得分:0)

您需要获取控件的值,尝试将其转换为日期(“尝试”,因为您永远不知道非日期是否可以进入此列),然后进行比较。

var playDate = DateTime.Parse(r.Cells(1).Text); // this could throw an error, research DateTime.TryParse()
if (DateTime.Now > playDate)
{
   // then...
}

对不起。刚刚意识到你的问题是VB。从{C#到VB'是Telerik's code converter,反之亦然。

答案 2 :(得分:0)

而不是

playDate = r.Cells(1).FindControl("playDate")

使用

playDate = r.Cells(1).Text;
只有在网格中有任何asp控件时,

FindControl才有效。对于boundfield,您需要获取单元格的值。