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

时间:2022-12-04 08:10:52

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

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

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

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

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)

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

1 个解决方案

#1


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!

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

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

<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);
        }
    }
}

#1


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!

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

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

<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);
        }
    }
}