Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / XHTML

Date picker in ASP.net gridview

2.66/5 (13 votes)
9 Feb 2010CPOL 1   2K  
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)