views:

632

answers:

1

I want to create an asp.net ajax calendar like this, but entirely in code-behind. How do I do it?

Edit: I only want to add the js code to the page in code-behind (eg: not in the markup)

Edit_2: I need this because I create a textbox control in a template class and want to create the calendar there (for use with the textbox)

A: 

The ASP.NET ajax calendar you refer to is an extender, so in theory you could add it to your textbox at runtime in the code-behind. See the example below, the placeholder is vital!

I'm not sure why you would want to do this, can you give us a better idea of the need as there may be a better solution.

Simple page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server"><asp:scriptmanager runat="server"></asp:scriptmanager>
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:PlaceHolder ID="Place1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CalendarExtender myCalExt = new CalendarExtender();
            myCalExt.TargetControlID = "TextBox1";
            Place1.Controls.Add(myCalExt);
        }
    }
}
Lazarus