如何动态创建 MaskedEditExtender?
我想从代码隐藏创建许多屏蔽编辑扩展器。 例如:
private MaskedEditExtender m_maskedEditExtender;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
m_maskedEditExtender = new MaskedEditExtender()
{
BehaviorID = "clientName"
};
m_maskedEditExtender.Mask = "999999999";
this.Controls.Add(m_maskedEditExtender);
}
protected override void Render(HtmlTextWriter writer)
{
m_maskedEditExtender.RenderControl(writer);
}
当我这样做时,我在 MaskedEditExtender 的 OnLoad 上收到 NullReferenceException。 这样做的正确方法是什么? 请注意,将扩展器放入类似中继器的控件中并使用 DataBind 对我来说不起作用。
编辑:我没有更新面板。 结果我还需要在服务器端指定一个目标控件。
I want to create a number of masked edit extenders from codebehind. Something like:
private MaskedEditExtender m_maskedEditExtender;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
m_maskedEditExtender = new MaskedEditExtender()
{
BehaviorID = "clientName"
};
m_maskedEditExtender.Mask = "999999999";
this.Controls.Add(m_maskedEditExtender);
}
protected override void Render(HtmlTextWriter writer)
{
m_maskedEditExtender.RenderControl(writer);
}
When I do this, I get a NullReferenceException on OnLoad of MaskedEditExtender. What is the correct way of doing that? Please note that putting the extender into a repeater-like control and using DataBind does not work for me.
Edit: I do not have an update panel. Turns out I also need to specify a target control on serverside.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例未提供 TargetControlID。
页面上有 updatePanel 吗? 我在动态创建扩展程序时遇到问题,因为它们没有添加到 updatePanel 内容中。
我还认为您必须使用 ScriptManager 做一些事情(注册扩展器),但我可能会错(我目前无法访问我执行动态扩展器的代码)。
Your example is not providing a TargetControlID.
Do you have an updatePanel on the page? I had problems dynamically creating extenders as they weren't being added to the updatePanel content.
I also think you have to do somethin with the ScriptManager (registering the extender) but I could be mistaken (I don't have access to the code I did dynamic extenders at the moment).
向 MaskedEditExtender 提供正确的 TargetControlID 值
Provide the proper TargetControlID value to MaskedEditExtender
如果页面位于页面中,请参阅 ASP.NET 页面生命周期概述子类。 如果您向下滚动到事件列表,该页面会建议您使用 PreInit 事件来创建任何动态控件。 除其他事项外,有必要尽早执行此操作,以确保 ASP.NET 在正确的阶段干净地加载 ViewState。
但是,如果您在 Web 用户控件或自定义控件中执行此操作,请重写 CreateChildControls 并在其中执行此操作。
如果没有帮助,请发布更完整的代码示例。
See ASP.NET Page Life Cycle Overview if this is in a Page subclass. If you scroll down to the event list, that page advises you to use the PreInit event to create any dynamic controls. It's necessary to do that early to ensure that ASP.NET cleanly loads ViewState at the right stage, among other things.
If you are doing this in a web user control or custom control, though, override CreateChildControls and do this in there.
Post a more complete code example if that doesn't help.