动态添加CalendarExtender到Textbox子类服务器控件?

发布于 2024-07-05 14:53:03 字数 359 浏览 7 评论 0原文

我正在尝试创建一个从 TextBox 继承的服务器控件,它将自动具有 CalendarExtender 附加到它。 是否可以这样做,或者我的新控件是否需要从 CompositeControl 继承? 我已经尝试过前者,但我不清楚应该在控件生命周期的哪一部分创建 CalendarExtender 的新实例,以及应该将其添加到哪个控件集合中。 我似乎无法将其添加到页面或表单的控件集合中,如果我将其添加到 (TextBox) 控件的集合中,我将无法获得任何弹出日历功能。

I'm trying to create a server control, which inherits from TextBox, that will automatically have a CalendarExtender attached to it. Is it possible to do this, or does my new control need to inherit from CompositeControl instead? I've tried the former, but I'm not clear during which part of the control lifecycle I should create the new instance of the CalendarExtender, and what controls collection I should add it to. I don't seem to be able to add it to the Page or Form's controls collection, and if I add it to the (TextBox) control's collection, I get none of the pop-up calendar functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

非要怀念 2024-07-12 14:53:03

我不久前在一个项目中完成了这一点。 为此,我创建了一个包含 TextBox 和 CalendarExtender 的 CompositeControl。

在 CompositeControl 的 CreateChildControls 方法中,我使用与此类似的代码:

TextBox textbox = new TextBox();
textbox.ID = this.ID + "Textbox";
textbox.Text = this.EditableField.TextValue;
textbox.TextChanged += new EventHandler(HandleTextboxTextChanged);
textbox.Width = new Unit(100, UnitType.Pixel);
CalendarExtender calExender = new CalendarExtender();
calExender.PopupButtonID = "Image1";
calExender.TargetControlID = textbox.ID;
this.Controls.Add(textbox);
this.Controls.Add(calExender);

当然,请确保包含此 CompositeControl 的表单具有工具包脚本管理器。

I accomplished this in a project a while back. To do it I created a CompositeControl that contains both the TextBox and the CalendarExtender.

In the CreateChildControls method of the CompositeControl I use code similar to this:

TextBox textbox = new TextBox();
textbox.ID = this.ID + "Textbox";
textbox.Text = this.EditableField.TextValue;
textbox.TextChanged += new EventHandler(HandleTextboxTextChanged);
textbox.Width = new Unit(100, UnitType.Pixel);
CalendarExtender calExender = new CalendarExtender();
calExender.PopupButtonID = "Image1";
calExender.TargetControlID = textbox.ID;
this.Controls.Add(textbox);
this.Controls.Add(calExender);

Of course make sure that the form containing this CompositeControl has a toolkit script manager.

大海や 2024-07-12 14:53:03

我知道这是一个旧线程,但当我有类似的问题时我遇到了它。 这就是我最终实现的,效果很好。 如果您希望控件成为 TextBox,则只需在调用 Render 期间抽出扩展器即可。

Imports System.Web.UI.WebControls
Imports AjaxControlToolkit

Public Class DateTextBox
    Inherits TextBox

    Private _dateValidator As CompareValidator
    Private _calendarExtender As CalendarExtender

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)

        _dateValidator = New CompareValidator
        With _dateValidator
            .ControlToValidate = ID
            Rem set your other properties
        End With
        Controls.Add(_dateValidator)

        _calendarExtender = New CalendarExtender
        With _calendarExtender
            .TargetControlID = ID
        End With
        Controls.Add(_calendarExtender)
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
        _dateValidator.RenderControl(writer)
        _calendarExtender.RenderControl(writer)
    End Sub
End Class

I know this is an old thread, but I came across it when I had a similar question. This is what I ended up implementing, and it works great. If you want the control to BE a TextBox, then simply pump out the extender during the call to Render.

Imports System.Web.UI.WebControls
Imports AjaxControlToolkit

Public Class DateTextBox
    Inherits TextBox

    Private _dateValidator As CompareValidator
    Private _calendarExtender As CalendarExtender

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)

        _dateValidator = New CompareValidator
        With _dateValidator
            .ControlToValidate = ID
            Rem set your other properties
        End With
        Controls.Add(_dateValidator)

        _calendarExtender = New CalendarExtender
        With _calendarExtender
            .TargetControlID = ID
        End With
        Controls.Add(_calendarExtender)
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
        _dateValidator.RenderControl(writer)
        _calendarExtender.RenderControl(writer)
    End Sub
End Class
舟遥客 2024-07-12 14:53:03

您可以轻松地在自定义服务器控件中添加 ajax 日历。 您需要在您的应用程序中添加两个参考。
1.AjaxControlToolkit.dll
2. 系统.Web.扩展
在第二个参考的帮助下,我们将在您的自定义服务器控件中获取“CalendarExtender”的所有属性。

You can easily add ajax calendar in custom server controls. You need to add two reference in your application.
1. AjaxControlToolkit.dll
2. System.Web.Extensions
With the help of second reference we will get all the property of “CalendarExtender” in your custom server controls.

℡Ms空城旧梦 2024-07-12 14:53:03

当您尝试不允许用户在文本框中键入任何内容,而仅由日历扩展器填充,然后尝试从文本框控件获取所选日期时,如果您已将文本框属性设置为只读,则它可能是空字符串=“真实”。

这是因为只读控件不会发布回服务器。 解决方法如下:

protected void Page_Load(object sender, EventArgs e)

{

TextBox1.Attributes.Add("readonly", "readonly");

}

希望能帮助到你。

When you are trying to not allow users to type anything in the textbox, but only be filled by the calendar extender and then you try to get the selected date from the textbox control it may be empty string if you have set the textbox property to ReadOnly="True".

Its because read only controls are NOT posted back to the server. Workaround for this is the following:

protected void Page_Load(object sender, EventArgs e)

{

TextBox1.Attributes.Add("readonly", "readonly");

}

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文