How to pass value of asp:Calendar to asp:TextBox, using JavaScript

Last post 01-23-2008 6:57 AM by kem06.net. 9 replies.

Sort Posts:

  • How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-22-2008, 10:23 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    I just wantto pass the value selected on my asp:Calendar to my asp:TextBox. I use the code following;

    <script language="javascript" type="text/javascript"> function TBYaz(elemTB, elemCLND)

    {

    elemTB.value=elemCLND.value;

    }

    </script>

    And i register the script as follows on Page_Load event of my webusercontrol as follows:

    if (!this.Page.IsClientScriptBlockRegistered("TBYaz"))

    {

    char i = '"';

    this.Page.RegisterClientScriptBlock("TBYaz",

    "<script language=" + i + "javascript" + i + "type=" + i + "text/javascript" + i + ">" +

    "function TBYaz(elemTB, elemCLND){" +

    "elemTB.value=elemCLND.value; }"

    );

    MyCalendar.Attributes.Add(
    "onchange", "TBYaz(document.getElementById('" + TBox.ClientID + "'),document.getElementById('" + MyCalendar.ClientID + "') )");

    }

     

    However, nothing happens. Or can i send the calendar to script function as parameter this way? Could someone pls helpme? 

  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-22-2008, 10:58 AM
    Answer
    • Star
      8,559 point Star
    • siva_sm
    • Member since 12-20-2007, 11:03 AM
    • Posts 1,256

    Calendar control is rendered as a table (<TABLE>) and it doesn't have an onchange event. Other option is to consider the SelectionChanged server-side event of the Calendar control and assign the selected date to the text box on the server-side itself. If you don't like the obvious postback page flicker, put the textbox and Calendar control inside an AJAX panel.

    Mark replies as answers if they helped you solve the problem.
  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-22-2008, 10:58 AM
    Answer

    The Calendar control does not have an onchange event.

    You have two choices:

    1. Use the server-side SelectionChanged event in the code-behind and do it all with server-side code, or
    2. Use the ASP.NET AJAX Control Toolkit and use the CalendarExtender.  http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx
    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-22-2008, 12:43 PM
    Answer

    I think you must try using the other Java script calender control instead of asp:calender control 

     find link below

    http://www.15seconds.com/issue/040315.htm 

    http://davidhayden.com/blog/dave/archive/2004/04/08/207.aspx

    or Google on the "Javascript calender control" 

    Nikhil Agrawal
    Think before you Think
  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-22-2008, 5:20 PM
    Answer
    • Participant
      756 point Participant
    • dotnetkode
    • Member since 01-22-2008, 9:47 PM
    • Herndon, VA
    • Posts 125

    I usually do this.

    If the calendar opens in a new window, pass the id of the text  box  that should be filled with date as a query string to the popup page the that contains the calendar window.

    In your popup page that contains the calendar

    Add OnSelectionChanged event to your calendar control  

    <asp:Calendar ID="SiteCalendar" runat="server" BackColor="White" BorderColor="#999999"
                        CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
                        ForeColor="Black" Height="180px" Width="200px" OnSelectionChanged="SiteCalendar_SelectionChanged">

    In code behind handle the event

    public void SiteCalendar_SelectionChanged(object sender, System.EventArgs e)
    {
            string date = SiteCalendar.SelectedDate.ToString("MM/dd/yyyy");
            string script = @"<script language=""javascript"">" +
                    "\n window.opener." + Request.QueryString["date"] + ".value = '" + date + "'; \n"  +
                    "window.close();" +
                    "</script>";
            //register this as startup script  so that it fires and populates the text box

     }

    If you have the calendar and the textbox on the same page, it is still simpler just do the following

    public void SiteCalendar_SelectionChanged(object sender, System.EventArgs e)
    {
            string date = SiteCalendar.SelectedDate.ToString("MM/dd/yyyy");
            txtDate.Text=date;  //txtDate is the id of the tetbox that should be filled with date.

     } 

     

    ~ Remember To Mark The Posts Which Helped You As The ANSWER ~
  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-23-2008, 3:21 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    Thank you all ppl. I wrote following code and it works fine, however, i don't really know how to  pick hour, set date format so that it should include hour aswell and change language of the calenderextender. Any idea would be appreciated.

    <asp:TextBox ID="TBIhaleBaslamaTarihi" runat="server" CssClass="input" ValidationGroup="ihalekle"></asp:TextBox>

    <cc1:CalendarExtender ID="CalExtBaslamaTar" runat="server" TargetControlID="TBIhaleBaslamaTarihi" Format="dd.MM.yyyy" >

    </cc1:CalendarExtender>

  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-23-2008, 3:47 AM

    can  use the Format="dd.MM.yyyy hh:mm:ss" (use HH/hh for 24/12 format)


    also refer for more details 

    http://msdn2.microsoft.com/en-us/library/az4se3k1.aspx 

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx 

    Nikhil Agrawal
    Think before you Think
  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-23-2008, 4:24 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    nikhil.agrawal:

    can  use the Format="dd.MM.yyyy hh:mm:ss" (use HH/hh for 24/12 format)


    also refer for more details 

    http://msdn2.microsoft.com/en-us/library/az4se3k1.aspx 

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx 

    Thanks.The format is ok, but how can i pick time on the calendarextender, 'cause only date selection is represented on it but not time selection? That is, how can i select time on the calendarextender?

    And also, it is ok that CultureInfo thing, but i need to change the language used in calendarextender aswell.

  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-23-2008, 6:24 AM

    Cool

    Sorry, you can't have the time selection in the Calander Extender.

    In that case you must look for the javscript calender control which are available with short googling. with keyword like "calender control with time  +  javascript"

     Pl. refer

    http://www.softcomplex.com/products/tigra_calendar/demo1.html 

     

    Nikhil Agrawal
    Think before you Think
  • Re: How to pass value of asp:Calendar to asp:TextBox, using JavaScript

    01-23-2008, 6:57 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    nikhil.agrawal:

    Cool

    Sorry, you can't have the time selection in the Calander Extender.

    In that case you must look for the javscript calender control which are available with short googling. with keyword like "calender control with time  +  javascript"

     Pl. refer

    http://www.softcomplex.com/products/tigra_calendar/demo1.html 

     

    Haha, i see. Thanks for the 'recommendation'  ;)

Page 1 of 1 (10 items)