如何在代码隐藏中创建日历?

时间:2009-04-23 12:17:29

标签: c# asp.net ajaxcontroltoolkit

我想创建一个像this这样的asp.net ajax日历,但完全在代码隐藏中。我该怎么做?

编辑:我只想在代码隐藏中将js代码添加到页面中(例如:不在标记中)

Edit_2:我需要这个,因为我在模板类中创建了一个文本框控件,并希望在那里创建日历(用于文本框)

1 个答案:

答案 0 :(得分:2)

您引用的ASP.NET ajax日历是一个扩展程序,因此从理论上讲,您可以在运行时在代码隐藏中将其添加到文本框中。请参阅下面的示例,占位符至关重要!

我不确定你为什么要这样做,你能否让我们更好地了解需求,因为可能有更好的解决方案。

简单页面

<%@ 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">

<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>

代码

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);
        }
    }
}
相关问题