2

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

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

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

4

1 回答 1

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);
        }
    }
}
于 2009-04-23T12:36:08.487 回答