Skip to content

[C#] Comparing dates in GridView

Chevy Mark Sunderland

Asked Apr 16, 2012, 10:49 AM EDT6 answers
Share
LinkedInX
Hi there, I hope your appreciated help.

First of all I must say that I am a newbie when it comes to net language.

This is my problem.

How to disabled the asp image button "ImageButton4" when today's date is greater than the registration date in my database ?.

For example the record with date registration '2012-04-14' should not display the asp image button "ImageButton4".

If you have link for similar task, please give it me.

Can you help me? thank you in advance.
Your help would be very appreciated

Thanks for your time and hints.

This is my code:

protected void ImageButton1_Click(object sender, EventArgs e)
{
ImageButton myButton = (ImageButton)sender;
string ID = myButton.CommandArgument.ToString();
Response.Redirect("http://localhost/WebApplication1/default.asp?id="; + ID + "&cks=" + Request.Cookies["MyCookie"].Value + "");

}


<ItemTemplate>
<asp:ImageButton ID="ImageButton4" CommandName="Redirect"
CommandArgument='<%#Eval("ID")%>' runat="server"
ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
OnClientClick="return confirm('Confirm?.');"
onclick="ImageButton1_Click" />
</ItemTemplate>

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Abhimanyu K Vatsa
    0
    Use the following concept 

    C#
    protected void Page_Load(object sender, EventArgs e)
    {
    DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
    DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
    int result = DateTime.Compare(date1, date2);
    
    
    if (result < 0)
       ImageButton4.Enable = false;
    else
       //do the rest part
    }
    0
  • 0
    thanks you, but with your suggestion I have error:
    ImageButton4 does not exist in current context


    public void Page_Load (Object sender, EventArgs e)
    {
        OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
        myConnectionString.Open();


        myConnectionString.Dispose();
        myConnectionString.Close();
       
        lblMessage.Text = "";
       
        DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
        DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
        int result = DateTime.Compare(date1, date2);


    if (result < 0) {
        ImageButton4.Enable = false;


    }


    }




    <ItemTemplate>
    <asp:ImageButton ID="ImageButton4" CommandName="Redirect" 
    CommandArgument='<%#Eval("ID")%>' runat="server"
    ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
    OnClientClick="return confirm('Confirm?.');"
    onclick="ImageButton1_Click" />
    </ItemTemplate>
    0
  • Senthilkumar
    0
    Hi,

    You requirement is very simple,

    <ItemTemplate>
    <asp:ImageButton ID="ImageButton4" CommandName="Redirect" 
    C#
    CommandArgument='<%#Eval("ID")%>' runat="server"
    ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
    OnClientClick="return confirm('Confirm?.');"
    onclick="ImageButton1_Click" />
    </ItemTemplate>

    what you need to do is, you have store the date from the database into hidden control or invisible label control.
    you need to write the Gridview row data bound.

    You need to have RowDataBound event,

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>

    In the server side,

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    Label lblDate = e.Row.Findcontrol("lblDate");
    ImageButton ImageButton4 = e.Row.FindControl("ImageButton4");
    DateTime dtDB = lblDate.Text;
    Datetime dtToday = DateTime.Now;
                    //compare the two dates
                     
    if(dtToday > dtDB)
                    {
     ImageButton2.Enabled = false;
                    }
            }
     }

    Hope this will help you.
    0
  • 0
    Hello Senthilkumar!
    thank you for your help.

    I try your suggestion but I have this error, why? I hope your help.

    Compiler Error Message: CS1061: 'System.Web.UI.WebControls.GridViewRow' does not contain a definition for 'Findcontrol' and no extension method 'Findcontrol' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRow' could be found (are you missing a using directive or an assembly reference?)


    <%@ Page Language="C#" Title="Home page" Culture="it-IT" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.Odbc" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.Services" %>
    <%@ Import Namespace="System.Web.Security" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
    <%@ Import Namespace="System.Web.UI.HtmlControls" %>



    Java
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblDate = e.Row.Findcontrol("lblDate");
            ImageButton ImageButton4 = e.Row.FindControl("ImageButton4");
            DateTime dtDB = lblDate.Text;
            Datetime dtToday = DateTime.Now;
    
    
            if (dtToday > dtDB)
            {
                ImageButton4.Enabled = false;
            }
        }
    
    
    }
    
    
    
    
    
    
    <asp:GridView ID="GridView1" runat="server"
        DataSourceID="SqlDataSource1"
        DataKeyNames="ID"
        AllowSorting="True"
        AllowPaging="True"
        PageSize="15"
        AutoGenerateColumns="false"
        CssClass="first"
        OnRowUpdated="GridView1_RowUpdated"
        OnRowDataBound="GridView1_RowDataBound">



    <ItemTemplate>
    <asp:ImageButton ID="ImageButton4" CommandName="Redirect" 
    C#
    CommandArgument='<%#Eval("ID")%>' runat="server"
    ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
    OnClientClick="return confirm('Confirm?.');"
    onclick="ImageButton1_Click" />
    </ItemTemplate>
    0
  • Senthilkumar
    0
    Hi chevy,

    You need to bind the data base date into lblDate control.
    <ItemTemplate>
    <asp:Label id="lblDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"datecolumnfromDB") %>' />
    </ItemTemplate>

    And also when you find the control,
     Label lblDate = (Label) e.Row.Findcontrol("lblDate");
     ImageButton ImageButton4 = (ImageButton) e.Row.FindControl("ImageButton4");
    0
  • 0
    Hello Senthilkumar!
    thank you for your help.

    I edit my net code with your suggestions but I have the new error.
    I hope your help.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error: 


    Line 117:        if (dtToday > dtDB)
    Line 118:        {
    Line 119:            ImageButton4.Enabled = false;
    Line 120:        }
    Line 121:    } 

    <asp:GridView ID="GridView1" runat="server" 
        DataSourceID="SqlDataSource1"
        DataKeyNames="ID"  
        AllowSorting="True" 
        AllowPaging="True" 
        PageSize="15"    
        AutoGenerateColumns="false" 
        CssClass="first" 
        OnRowUpdated="GridView1_RowUpdated"
        OnRowDataBound="GridView1_RowDataBound">




      <asp:TemplateField HeaderText="myDate" SortExpression="myDate">
        <ItemTemplate>
          <asp:Panel ID="Panel2" Runat="Server">
            <asp:Label ID="Label2" Text='<%# Eval("myDate", "{0:dd/MM/yyyy}") %>' Runat="Server"/>
          </asp:Panel>
        </ItemTemplate>
      </asp:TemplateField>  




    <ItemTemplate>
       <asp:ImageButton ID="ImageButton4" CommandName="Redirect" 
            CommandArgument='<%#Eval("ID")%>' runat="server"
            ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
            OnClientClick="return confirm('Confirm?.');"
            onclick="ImageButton1_Click" />
    </ItemTemplate>






    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label Label2 = (Label)e.Row.FindControl("Label2");
            ImageButton ImageButton4 = (ImageButton)e.Row.FindControl("ImageButton4");
           
            
    DateTime dtDB = DateTime.ParseExact(Label2.Text, "dd/MM/yyyy", null);
            DateTime dtToday = DateTime.Now;
         


            if (dtToday > dtDB)
            {
                ImageButton4.Enabled = false;
            }
        }    
       
    }  
    0