C# .NET - Date picker control inside gridview textbox.

Asked By Rahul Khanna
20-May-11 03:29 AM

Hi all,

I got  one link how to display jquery date picker control and display the date inside a textbox. Its workinfine in normaltextbox. Its working like if i am clikcing on textbox calender is opening and after selecting the date date is coming to texbox field.  My requirement is i have a gridview there i am dyanmically adding the row. There one field is date field which  is texbox. I want when user click on txtDateField gridview calender should open and after date selection data should come gridviewdatetexbox field. HOw can i do this any help.

Here is the code:

1.  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestjQueryUICDN.WebForm1" %>  

2.  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

3.  <html xmlns="http://www.w3.org/1999/xhtml">  

4.  <head runat="server">  

5.    <title>Using jQuery UI from the CDN</title>  

6.    <link type="text/css" rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />  

7.  </head>  

8.  <body>  

9.    <form id="form1" runat="server">  

10.   <div>  

11.     

12.     <asp:TextBox ID="txtStartDate" ClientIDMode="Static" runat="server" />  

13.   

14.   </div>  

15.   </form>  

16.   

17.   <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.js"></script>  

18.   <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>  

19.   

20.   <script type="text/javascript">  

21.   

22.     $("#txtStartDate").datepicker();  

23.   

24.   </script>  

25. </body>  

26. </html>  

 

  TSN ... replied to Rahul Khanna
20-May-11 04:23 AM
hi..

Look at thi example...

GridView Markup
To start with I have a simple GridView with 2 columns where the first column is the name of the person and the second column is a TemplateField with ReadOnly TextBox to enter select the Date and time of birth of the person.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
      <asp:BoundField DataField="Name" HeaderText="Person" />
      <asp:TemplateField HeaderText="Date Of Birth">
        <ItemTemplate>
          <asp:TextBox ID="txtDOB" runat="server" ReadOnly="true" class = "Calender" />
          <img src="calender.png" />
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />

You will notice I have made the TextBox that accepts the date and time of birth ReadOnly. I have done so to prevent the users from typing the text in the TextBox. I have also added class = "Calender" attribute to the TextBox so that I can select the TextBox for dates using jQuery Selector
Finally there’s a button to Save the Records to the database. Later in the article I’ll describe how to fetch the dates from the GridView server side
Applying the DateTimePicker jQuery Plugin
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<script type = "text/javascript">
    $(document).ready(function () {
      $(".Calender").dynDateTime({
        showsTime: true,
        ifFormat: "%Y/%m/%d %H:%M",
        daFormat: "%l;%M %p, %e %m, %Y",
        align: "BR",
        electric: false,
        singleClick: false,
        displayArea: ".siblings('.dtcDisplayArea')",
        button: ".next()"
      });
    });
</script>

You need to place the above script either on the page or the MasterPage where you want apply the DateTimePicker plugin. This DateTimePicker will automatically be applied to all TextBoxes who have attribute class = "Calender"
Screenshot
That’s all you need to do to apply the DateTimePicker jQuery plugin in ASP.Net GridView control.
DateTimePicker in DataGrid using jQuery DateTimePicker Plugin
Fetching the Dates Server Side
Below is how you can fetch the dates that the user has submitted by looping through the rows of the GridView Control
C#
protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
      DateTime dob = DateTime.Parse(Request.Form[row.FindControl("txtDOB").UniqueID]);
      //Save the date to Database here
    }
}
  Riley K replied to Rahul Khanna
20-May-11 04:29 AM
To display datepicker inside gridview see below code

<asp:GridView ID="GridViewCompleted" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
    BorderWidth="3px" CellPadding="4" DataKeyNames="job_id" DataSourceID="SourceJobProjectCompleted"
    ForeColor="Black" CellSpacing="2">
<Columns>
...more columns here...
<asp:TemplateField HeaderText="Completed Date" SortExpression="actual_completed_date" ItemStyle-HorizontalAlign="Center">
        <EditItemTemplate>
          <asp:TextBox ID="txtActual_completed_date" runat="server" Text='<%# Bind("actual_completed_date") %>'
            Width="60px" />
        </EditItemTemplate>
        <ItemTemplate>
          <asp:Label ID="LabelActual_completed_date" runat="server" Text='<%# Bind("actual_completed_date") %>' />
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
      </asp:TemplateField>
</Columns>
</asp:GridView>

<script type="text/javascript">
        $
(function () {
            $
("[id$=_txtActual_completed_date]").datepicker({ showOtherMonths: true, selectOtherMonths: true, minDate: '-1m', maxDate: '+1m'
           
});
       
});
   
</script>
   
<script type="text/javascript">
        $
(function () {
            $
("[id$=_TextBoxDateFrom]").datepicker({ showOtherMonths: true, selectOtherMonths: true, minDate: '-6m', maxDate: '+6m'
           
});
       
});
   
</script>
   
<script type="text/javascript">
        $
(function () {
            $
("[id$=_TextBoxDateTo]").datepicker({ showOtherMonths: true, selectOtherMonths: true, minDate: '-6m', maxDate: '+6m'
           
});
       
});
   
</script>
Use the above way
  Rahul Khanna replied to TSN ...
20-May-11 04:52 AM
thanks for your reply...by the way do i need to downlaod the below files if yes where can i download??

script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
  Rahul Khanna replied to Riley K
20-May-11 05:00 AM
Hi a am getting this exception while running applying your code:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'txtActual_completed_date'.
  TSN ... replied to Rahul Khanna
20-May-11 10:56 AM

hi..

http://code.google.com/p/dyndatetime/updates/list
  Kristof replied to Rahul Khanna
12-Jul-11 06:29 AM
Yes, this really helped me out ! Thanks a lot.
Create New Account
help
label and set its Text property like this. protected void Griddata_RowDataBound(object sender, GridViewRowEventArgs e) { DataRowView objDRV = (DataRowView)e.Row.DataItem; Label lbl = e.Row.FindControl("lblGender") as Label; if(lbl! = null) { if ToUpper().Equals("MALE")) { lbl.Text = "Gent"; } else lbl.Text = "Lady"; } } Add the following type of TemplateField in your GridView. <asp:TemplateField> <ItemTemplate> <asp:Label ID = "lblGender" runat = "server"> < / asp:Label> < / ItemTemplate> < / asp:TemplateField> bro whr i have to take a label . . ???? add one Lable in TEmplate that already ForeColor = "White" / > <HeaderStyle BackColor = "#000084" Font-Bold = "True" ForeColor = "White" / > <AlternatingRowStyle BackColor = "#DCDCDC" / > <Columns> <asp:TemplateField HeaderText = "Name" > <ItemTemplate> <asp:Label Text = '<%#Bind("name") %> ' ID = "lblname" runat = "server" > < / asp:Label> < / ItemTemplate> < / asp:TemplateField> <asp:TemplateField HeaderText = "Gender"> <ItemTemplate> <asp:Label Text = '<%#Bind("gender") %> ' ID = "lblgender" runat = "server"> < / asp:Label> < / ItemTemplate
error DataBinding: 'System.Data.DataRowView' not contain "categoryid" plz help me for the error DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'categoryid'. plz do help me regards gaurav GridView1" runat = "server" CellPadding = "4" ForeColor = "#333333" DataKeyNames = "categoryid" GridLines = "None" OnRowCommand = "GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText = "Delete"> <ItemTemplate> <asp:LinkButton ID = "LinkButton1" CommandName = "Delete" runat = "server"> Delete< / asp:LinkButton> < / ItemTemplate> < / asp:TemplateField> <asp:TemplateField HeaderText = "Register"> <ItemTemplate> <asp:LinkButton ID = "LinkButton2" CommandName = "Register" runat = "server"> Register< / asp:LinkButton> < / ItemTemplate asp:TemplateField> < / Columns> codebehind: using System; using System.Collections; using System.Configuration; using System.Data; using System ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); if (e.CommandName = = "Delete") { string id = ((Label)row.FindControl("lblid")).Text
runat = "server" AutoGenerateColumns = "false" CssClass = "Grid" DataKeyNames = "SUBJECTID, CLASSID" Caption = "SUBJECTS" Width = "100%" > < Columns > < asp:TemplateField HeaderText = "#" > < ItemTemplate > <%# Container.DataItemIndex + 1 %> < / ItemTemplate > < ItemStyle Width = "5%" / > < HeaderStyle ForeColor = "Blue" / > < / asp:TemplateField > < asp:TemplateField HeaderText = "SUB-MAIN" > < ItemTemplate > < asp:LinkButton ID = "LinkButton1" runat = "server" Text = '<%# Eval("SUBJECTNAME") %> '> < / asp:LinkButton ItemTemplate > < / asp:TemplateField > < asp:BoundField DataField = "CLASSID" HeaderText = "CLASSID" / > < asp:TemplateField HeaderText = "Details" > < ItemTemplate > < asp:GridView ID = "GridView2" runat = "server" Width = "100%" AutoGenerateColumns = "false" > < Columns > < asp 20%" / > < asp:BoundField DataField = "PAPERTYPE" HeaderText = "TYPE" ItemStyle-Width = "20%" / > < / Columns > < / asp:GridView > < / ItemTemplate > < / asp:TemplateField > < / Columns > < / asp:GridView > in c# code, if in a button click 01. protected void btns_Click new DataTable(); 08. adp.Fill(dt); 09. GridView1.DataSource = dt; 10. GridView1.DataBind(); 11. foreach (GridViewRow row in GridView1.Rows) 12. { 13. LinkButton ln = (LinkButton)row.FindControl( "LinkButton1" ); 14. cmd = new HorizontalAlign = "Left" Wrap = "True" > < / ItemStyle > < ItemTemplate > < asp:Repeater ID = "rptChild" runat = "server" DataSource = '<%# ((System.Data.DataRowView) Container.DataItem).Row. GetChildRows("relationParentChild") %> '> < ItemTemplate > < asp:LinkButton ID = "linkChild" Runat = "server" CommandArgument = <%# DataBinder. Eval
Datenbindungs Ausdruck kann ich eine Function aufrufen und Parameter übergeben: Aus Container kann ich die DataRowView und die GridViewRow ermitteln. Welchen Parameter kann ich übergeben, um die aktuelle GridView Column, also den Index der String End Function Lutz ASP.NET - German Discussions System.Web.UI.IDataItemContainer (1) IDataItemContainer (1) GridViewRow (1) DataRowView (1) Container.DataItem (1) TemplateField (1) DataBinder (1) GridView (1) Hallo Lutz, welchem Zweck sollte das dienen? Willst Du die miteinander zu tun, außer dem was hinten raus kommt. . . Ich grüble gerade, wie man ein TemplateField mit datengebundenem Label per Code dem GridView hinzu fügen sollte??? Label9.Text = "<%# Bind(""Name"") %> " Lutz ermitteln description: Im GridView Datenbindungs Ausdruck kann ich eine Function aufrufen und Parameter übergeben: asp:TemplateField ItemTemplate%#Kw(Container)
Text = "SingleClick" CommandName = "SingleClick" Visible = "false" / > < asp : ButtonField Text = "DoubleClick" CommandName = "DoubleClick" Visible = "false" / > < asp : TemplateField HeaderText = "ENo."> < ItemTemplate > <% # Eval( "Sno" ) %> < / ItemTemplate > < / asp : TemplateField > < asp : TemplateField HeaderText = "Name"> < ItemTemplate > <% # Eval( "Name" ) %> < / ItemTemplate > < / asp : TemplateField > < asp : TemplateField HeaderText = "Salary"> < ItemTemplate > <% # Eval( "Salary" ) %> < / ItemTemplate > < / asp : TemplateField > < asp : TemplateField HeaderText = "DeptNo"> < ItemTemplate > <% # Eval( "DeptNo" ) %> < / ItemTemplate > < / asp TemplateField > < asp : TemplateField HeaderText = "Address"> < ItemTemplate > <% # Eval( "Address" ) %> < / ItemTemplate > < / asp : TemplateField > < asp : TemplateField HeaderText = "Country"> < ItemTemplate