【问题标题】:Check for expired date in Gridview在 Gridview 中检查过期日期
【发布时间】:2018-01-23 13:51:03
【问题描述】:

我对编程和调整已经为我编写的一些代码还很陌生。我想检查 Gridview(named: 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

【问题讨论】:

  • 您将playDate 定义为Date 然后由playDate = r.Cells(1).FindControl("playDate") 使用Control 启动它,这是不正确的。
  • 改成playDate = r.Cells(1).Text

标签: asp.net vb.net


【解决方案1】:

您将 playDate 定义为 Date 然后使用 Control by 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 程序员,但我确信我的解释是正确的,修改后的代码也可以。

【讨论】:

  • 工作就像一个魅力.. 非常感谢!
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    代替

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

    使用

    playDate = r.Cells(1).Text;
    

    FindControl 仅在网格中有任何 asp 控件时才有效。对于boundfield,需要获取cell的值。

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      相关资源
      最近更新 更多