Click here to Skip to main content
15,867,568 members
Articles / Web Development / XHTML

Date picker in ASP.net gridview

Rate me:
Please Sign up or sign in to vote.
2.66/5 (13 votes)
9 Feb 2010CPOL 66.4K   2K   12   5
Datet picker in ASP.net gridview

introduction

this code eases the implementation of adding datetime picker in gridview ,this uses built in controls of asp.net without any 3rd party tools,the solves problems like editing date values inside the gridview.


using the code

before getting into working nature of code let me explain u about the trick i used in this , getting clientid of particular cells textbox in rowediting event and registrering along with javascript gives exact needs to set datetime picker in gridview

        protected void gridview1_rowediting(object sender, gridviewediteventargs e)
{
    int f;
    f = e.neweditindex;
    f += 2;

    string clientid = "ctl00_contentplaceholder1_gridview1_ctl";

    string startdate;
    if (f <= 9)
    {
        startdate = clientid + "0" + f.tostring() + "_txt_when";
    }
    else
    {
        startdate = clientid +  f.tostring() + "_txt_when";
    }


    string targetdate;
    if (f <= 9)
    {
        targetdate = clientid + "0" + f.tostring() + "_txt_tdate";
    }
    else
    {
        targetdate = clientid +  f.tostring() + "_txt_tdate";
    }


    string script;
    script = "<script language=" + (char)34 + "javascript" + (char)34 + "  >";
    script += "function getdate(result,ctrl)";
    script += "{";
    script += "if(ctrl == 'txt_when' )";
    script += "{";
    script += "document.getelementbyid(" + (char)34 + startdate + (char)34 + ").value      = result;";
    script += "document.getelementbyid(" + (char)34 + startdate + (char)34 + ").innertext  = result;";
    script += "document.getelementbyid(" + (char)34 + startdate + (char)34 + ").readonly   = true;";
    script += "}";
    script += "else";
    script += "{";
    script += "document.getelementbyid(" + (char)34 + targetdate + (char)34 + ").value      = result;";
    script += "document.getelementbyid(" + (char)34 + targetdate + (char)34 + ").innertext  = result;";
    script += "document.getelementbyid(" + (char)34 + targetdate + (char)34 + ").readonly   = true;";
    script += "}";
    script += "}";
    script += "</script>";
    l1.text = script;
}

points of interest

the intresting facts in this article is to use of dynamic loading controls clientid and literal controls to register javascript.

history

keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthanks for sharing - have 5 Pin
Pranay Rana30-Jan-11 18:54
professionalPranay Rana30-Jan-11 18:54 
GeneralAWESOME Pin
simo50029-Mar-10 16:44
simo50029-Mar-10 16:44 
Generalcould not download the code Pin
Muniappan27-Jan-09 10:19
Muniappan27-Jan-09 10:19 
GeneralMy vote of 2 Pin
Mohan.Net10-Dec-08 0:49
Mohan.Net10-Dec-08 0:49 
Generalstupid thing where is the time Ediote Pin
gsqvxqbx1-Jun-08 6:04
gsqvxqbx1-Jun-08 6:04 

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.