SIGN UP MEMBER LOGIN:    
ARTICLE

Calender Control in an ASP.NET 2.0 GridView Control

Posted by Srinivas Articles | ASP.NET Programming May 08, 2009
The code attached in this article shows how to integrate a Calender control in the GridView control using ASP.NET 2.0.
Reader Level:

This is very easy to integrate a GridView control to Java Script Calender Control.

The code will be looking like this in GridView template.

In the gridview control every row in the grid view return the diffrent id for the calender control. We need to catch the calender id at runtime.

<
asp:TemplateField SortExpression="Purchase_Dt" HeaderText="Purchase Date">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Wrap="False"/>
<EditItemTemplate>
<asp:TextBox ID="txt_Purchase_Dt" Width="100" Runat="server" Text='<%# Bind("Purchase_Dt","{0:dd/MM/yyyy}") %>'></asp:TextBox><asp:ImageButton ID="img_date" CommandName="img_date1" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ImageUrl="~/cal.gif" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPurchase_Dt" runat="server" Width="100"></asp:TextBox><asp:ImageButton ID="img_date_Footer" CommandName="img_date_Footer" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ImageUrl="~/cal.gif" runat="server" />
</FooterTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("Purchase_Dt","{0:dd/MM/yyyy}") %>' ID="lbl_Purchase_Dt"></asp:Label>
</ItemTemplate>
</asp:TemplateField>


Then open the RowDataItem event of the Gridview control here we can get the ID of the calender control at runtime in grid view control.

The code looking like this.


protected
void g1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowState == DataControlRowState.Edit || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
  {
    ImageButton i1 = (ImageButton)e.Row.FindControl("img_date");
    TextBox t1 = (TextBox)e.Row.FindControl("txt_Purchase_Dt");
    i1.Attributes.Add(
"OnClick", "return f1(" + t1.ClientID + ")");
  }
  else if (e.Row.RowType== DataControlRowType.Footer)
  {
    if (e.Row.RowState == DataControlRowState.Normal)
    {
      ImageButton i1 = (ImageButton)e.Row.FindControl("img_date_Footer");
      TextBox t1 = (TextBox)e.Row.FindControl("txtPurchase_Dt");
      i1.Attributes.Add(
"OnClick", "return f1(" + t1.ClientID + ")");
      LinkButton L2 = (LinkButton)e.Row.Cells[0].FindControl("LinkButton2");
      TextBox txt_AvalidQnty = (TextBox)e.Row.FindControl("txtLic_Avlbl_qty");
      TextBox txt_PurDate = (TextBox)e.Row.FindControl("txtPurchase_Dt");
      TextBox txt_SoftDesc = (TextBox)e.Row.FindControl("txtSoftware_Desc");
      //TextBox t3 = (TextBox)e.Row.FindControl("txt_Lic_Avlbl_qty");
      L2.Attributes.Add(
"OnClick", "return Validate(" + txt_SoftDesc.ClientID + "," + txt_PurDate.ClientID + "," + txt_AvalidQnty.ClientID + ")");
      //TextBox txt_PurDate = (TextBox)e.Row.FindControl("txtPurchase_Dt");
      //L2.Attributes.Add("OnClick", "return Validate_PurchaseDate(" + txt_PurDate.ClientID + ")");
      //TextBox txt_SoftDesc = (TextBox)e.Row.FindControl("txtSoftware_Desc");
      //L2.Attributes.Add("OnClick", "return Validate_SoftDesc(" + txt_SoftDesc.ClientID + ")");
    }

 

The java function f1 code is following:


function
f1(Object)
{
    //alert(Object);
    showCalendarControl(Object);
    return false;
}


This function is the java script calender control

showCalendarControl(Object);

This is the code I have written in my application.


Login to add your contents and source code to this article
share this article :
post comment
 

its not working

Posted by Abhijeet Sinha May 10, 2010

nice work but u may save time and effort instead of doing coding on row data bind you may use 2 options:
1) use Microsoft Ajax Calender extender control
2) in case you don't like option 1 you may use jQuery and date time  picker plugin or any Calender plugins and mark required textbox control with specific Css Class that mapped with jquery plugin.


Posted by Mohammed Jun 02, 2009

Dear brother's,

This article is very good and very helpful to me. thanks brother to share a great knowlodge. I hope you will keep it.

Posted by waqar laghari May 11, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor