Click here to Skip to main content
Licence CPOL
First Posted 30 Apr 2008
Views 30,078
Downloads 847
Bookmarked 10 times

Date picker in ASP.net gridview

By | 9 Feb 2010 | Article
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)

About the Author

i gr8

Software Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalthanks for sharing - have 5 PinmemberPranay Rana18:54 30 Jan '11  
GeneralAWESOME Pinmembersimo500216:44 9 Mar '10  
Generalcould not download the code PinmemberMuniappan10:19 27 Jan '09  
could not download the code. Help me please?
GeneralMy vote of 2 Pinmembermohannet0:49 10 Dec '08  
Generalstupid thing where is the time Ediote Pinmembergsqvxqbx6:04 1 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120604.1 | Last Updated 9 Feb 2010
Article Copyright 2008 by i gr8
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid