Click here to Skip to main content
14,131,177 members
Click here to Skip to main content
Add your own
alternative version

Stats

60.2K views
1.9K downloads
12 bookmarked
Posted 30 Apr 2008
Licenced CPOL

Date picker in ASP.net gridview

, 9 Feb 2010
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

i gr8
Software Developer
India India
No Biography provided

You may also be interested in...

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
membersimo50029-Mar-10 16:44 
Generalcould not download the code Pin
Muniappan27-Jan-09 10:19
memberMuniappan27-Jan-09 10:19 
GeneralMy vote of 2 Pin
Mohan.Net10-Dec-08 0:49
memberMohan.Net10-Dec-08 0:49 
Generalstupid thing where is the time Ediote Pin
gsqvxqbx1-Jun-08 6:04
membergsqvxqbx1-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190518.1 | Last Updated 9 Feb 2010
Article Copyright 2008 by i gr8
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid