65.9K
CodeProject is changing. Read more.
Home

GridView needs more plumbing code to support Calendar controls

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (6 votes)

Nov 28, 2007

viewsIcon

27270

downloadIcon

358

GridView needs more plumbing code to support Calendar controls

Download TaskManager.zip - 55.4 KB

GridView needs more plumbing code to support Calendar controls

Introduction

I needed to create a TaskManager application to manage tasks assigned to programmers in my team and used calendar controls to provide the StartDate and EndDate for each task created. I am able provide two way binding for SelectedDate and also retain the VisibleDate as documented on MSDN, but I had problems retaining the SelectedDate in the GridView during edits.

Background

Although I have EnableViewState set to True, I had to store the StartDate and EndDate in ViewState collection as ViewState ["StartDate"] and ViewState ["EndDate"] and assign them to SelectedDate during page refresh. The code to do this is straight forward and is shown below.

Screenshot - TaskManager.jpg

Using the Code

<code> 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (ViewState["StartDate"] != null) 
    { 
        ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[5].Controls[1]).SelectedDate = (DateTime)ViewState["StartDate"]; 
    } 
    if (ViewState["EndDate"] != null) 
    { 
        ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[6].Controls[1]).SelectedDate = (DateTime)ViewState["EndDate"]; 
    } 
} 
</code>
<code> 
protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
{ 
    ViewState["StartDate"] = ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[5].Controls[1]).SelectedDate; 
} 
</code>
<code> 
protected void Calendar2_SelectionChanged(object sender, EventArgs e) 
{ 
    ViewState["EndDate"] = ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[6].Controls[1]).SelectedDate; 
} 
</code>
<code> 
protected void odsTaskManager_Updating(object sender, SqlDataSourceCommandEventArgs e) 
{ 
    if (ViewState["StartDate"] != null) 
    { 
        e.Command.Parameters["@StartDate"].Value = ((DateTime)ViewState["StartDate"]).ToString(); 
        ViewState.Remove("StartDate"); 
    } 
    if (ViewState["EndDate"] != null) 
    { 
        e.Command.Parameters["@EndDate"].Value = ((DateTime)ViewState["EndDate"]).ToString(); 
        ViewState.Remove("EndDate"); 
    } 
    if (txtTaskDescription.Text.Length >