Search between 2 dates and update a GridView with ObjectDataSource

604 views Asked by At

I am writing an ASP.NET application that has a GridView that uses an ObjectDataSource to fill it with data from a list of objects that is populated by a REST call. I also need to be able to search between 2 dates using a "Search" button which requires me to make a REST call to another URL. The data has the same structure, it's just set between 2 dates.

This is the code I'm using to set the DataGrid, this is the SorRepository class. The method Select() is being used as the Select code for my ObjectDataSource:

namespace SorDowntimeWebApp.Models
{
    public class SorRepository
    {
        public List<SorEvent> Select()
        {
            // this URL gets the last 30 days of information
            string url = new WebClient().DownloadString(@"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/History/30");
            List<SorEvent> events = JsonConvert.DeserializeObject<List<SorEvent>>(url);
            events = events.OrderByDescending(e => e.StartTime).ToList();
            return events;
        }
    }
}

This is the SorEvent class that the above method uses:

public class SorEvent
{
    public long Id { get; set; }
    public string Furnace { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime? EndTime { get; set; }
    public string MachineCode { get; set; }
    public string ReasonCode { get; set; }
    public string SubsystemCode { get; set; }
    public string ForceScheduleFlag { get; set; }
    public string OperatorComments { get; set; }
}

That part works fine. I just need to be able to update the GridView after the user selects 2 dates.

This is the code I've tried so far on the button click, the buttons ID is btnSearch:

protected void btnSearch_Click(object sender, EventArgs e)
{
    string startDate = txtStartDate.Text;
    string endDate = txtEndDate.Text;
    // this url gets the data from a specified range
    string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/HistoryByDate?startDate=" +
                 startDate + "&endDate=" + endDate;
    List<SorEvent> events = JsonConvert.DeserializeObject<List<SorEvent>>(new WebClient().DownloadString(url));
    GridView1.DataSource = events;
    GridView1.DataBind();
}

txtStartDate & txtEndDate are both text boxes that use the Jquery UI date picker.

I tried using an Update function in my SorRepository class but that didn't work.

Also, please note that when I perform this search and call the url, it will be using the same SorEvent class as the Select() method. The data grid will have the same columns.

1

There are 1 answers

0
user On BEST ANSWER

This is the solution I came up with. I needed to remove the ObjectDataSource from the DataSourceId and set the data for the GridView in the Page_Load method. Here's my code now:

public partial class SORGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/History/30";
        List<SorEvent> events = JsonConvert.DeserializeObject<List<SorEvent>>(new WebClient().DownloadString(url));
        events = events.OrderByDescending(ev => ev.StartTime).ToList();
        if (!IsPostBack)
        {
            GridView1.DataSource = events;
            GridView1.DataBind();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        // get dates and call the url with those 2 dates (POST call)
        string startDate = txtStartDate.Text;
        string endDate = txtEndDate.Text;
        string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/History/30";
        if(startDate != "" || endDate != "")
            url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/HistoryByDate?startDate=" +
                     startDate + "&endDate=" + endDate;
        List<SorEvent> events = JsonConvert.DeserializeObject<List<SorEvent>>(new WebClient().DownloadString(url));
        events = events.OrderByDescending(ev => ev.StartTime).ToList();
        if (IsPostBack)
        {
            GridView1.DataSource = events;
            GridView1.DataBind();
        }

    }
}