Click here to Skip to main content
15,867,974 members
Articles / Web Development / ASP.NET
Article

GridView needs more plumbing code to support Calendar controls

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
27 Nov 2007 27K   358   26   1
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

ASP.NET
<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>
ASP.NET
<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 >

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Viswanath Majeti works as a Project Lead in .NET Technologies for a software development company in Hyderabad, India

Comments and Discussions

 
Questionhow to run the project?database file is missing Pin
bdmukta31-Aug-09 5:58
bdmukta31-Aug-09 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.