Generating a List of DateTime objects for a given date range

.NET, Programming No Comments »

Here’s a little C# method I created recently. I needed a way to get all the dates between 2 dates as objects for use in a custom calendar control.

    private static List<DateTime> CreateRange(DateTime startDate, DateTime endDate)
    {
        List<DateTime> r = new List<DateTime>();
 
        int startYear = startDate.Year;
        int startMonth = startDate.Month;
        int endYear = endDate.Year;
        int endMonth = endDate.Month;
        const int day = 1;
        const int totalMonths = 12;
 
        int totalYears = endYear - startYear;
        
        if (totalYears > 0)
        {
            endMonth += totalYears * totalMonths;
        }
 
        for (int year = startYear; year <= endYear; year++)
        {
            for (int mth = startMonth; mth <= endMonth; mth++)
            {
                int month = mth;
 
                if (month > totalMonths)
                {
                    month -= totalMonths;
                }
 
                r.Add(new DateTime(year, month, day));
 
                if (mth == totalMonths)
                {
                    endMonth -= totalMonths;
                    startMonth = 1;
                    break;
                }
            }
        }
 
        r.Reverse();
 
        return r;
    }
 
 

Now with the results of this method, you have a populated generic

List of DateTime objects between any 2 dates.

ASP.NET "Invalid postback or callback argument." Exception Solved

.NET, Programming No Comments »

I’ve been wracking my brains out for 2 days on this exception, until now.

I have a Web User Control that contains a GridView with some Command Buttons using an ObjectDataSource. Pretty simple. Let’s call it ActiveWidgets.ascx.

I wanted to notify the parent page when either of the command buttons where clicked in ActiveWidgets to notify other controls on the page that they need to re-bind or whatever.

Then when I’d click the Edit or Delete command button, boom, Invalid Postback exception.

I have the command buttons in a template field so I could add a javascript onclientclick confirmation to the Delete button. At first I thought this had something to do with the problem, but no.

It turns out that since I overrode DataBind in ActiveWidges.ascx, that method was getting called multiple times during the page load process. This was causing Invalid Postback exception.

Naturally I was using the DataBind method to call my logic layer to get the records to populate the GridView.

To solve the problem, I just changed my DataBind method from:

protected override void DataBind()
{
// Logic stuff
// DataBinding controls…
}
to:
public void Bind()
{
// Same Logic Stuff
// Same DataBinding controls
}

Now I know I’m the only one calling Bind and my button commands are firing perfectly.

I hope this helps you with any similar problems you may be experiencing. Please leave a comment to let me know :)

Technorati tags: , , ,
Design by j david macor.com.Original WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login