动态添加CalendarExtender到Textbox子类服务器控件?
我正在尝试创建一个从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不久前在一个项目中完成了这一点。 为此,我创建了一个包含 TextBox 和 CalendarExtender 的 CompositeControl。
在 CompositeControl 的 CreateChildControls 方法中,我使用与此类似的代码:
当然,请确保包含此 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:Of course make sure that the form containing this CompositeControl has a toolkit script manager.
我知道这是一个旧线程,但当我有类似的问题时我遇到了它。 这就是我最终实现的,效果很好。 如果您希望控件成为 TextBox,则只需在调用 Render 期间抽出扩展器即可。
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.
您可以轻松地在自定义服务器控件中添加 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.
当您尝试不允许用户在文本框中键入任何内容,而仅由日历扩展器填充,然后尝试从文本框控件获取所选日期时,如果您已将文本框属性设置为只读,则它可能是空字符串=“真实”。
这是因为只读控件不会发布回服务器。 解决方法如下:
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.