Search code examples
asp.netvisibleinvisible

ASP.NET:Button Invisible after specific time period


I am developing an application where i need the following requirement:

Suppose i have a button (initially Enable ) . i want after 7 days period it will goes to invisible mode .

P.S. : 1) if User does not click button that time it also goes to invisible after specific time period.

`<asp:Button ID="btnup" Text="Update" runat="server" OnClick="OpenWindow" style="font-weight: bold"  />`

Solution

  • Of course you need to save the date somewhere so you can check that date with the date of today."so let say database"
    For testing also make an label on the page so you can see the value of the showing the result of the subtract.
    So first you need to get the 2 dates "database datetime and datetime now"
    Then you use a timespan to subtract the 2 dates from each other.
    Then you make a double and change the timespan into only showing the days.

    Then you make and if statement and check if the double is smaller then -7 "or what value you want " then hide the button.

    DateTime dtn = DateTime.Now;
    DateTime dtl = DBDateTime;
    TimeSpan span = new TimeSpan();
    span = dtl.Subtract(dtn);
    double numDays = span.Days;
    
    if (numDays < -7)
    {
    Button1.Visible = false;                            
    }
    DaysLeftTB.Text = numDays.ToString();