自定义 Web 控制和回发
我正在尝试创建一些包装一些现有控件的自定义 Web 控件。例如,其中之一是文本框的包装(因为我需要其他行为,例如相应的验证器)。问题是,我不确定如何获取要在回发中发送的该控件的数据。
一个非常简单的示例:
public class MyTestBox: WebControl
{
protected TB { get; set; }
public Text
{
get { return TB.Text; }
set { TB.Text = value; }
}
protected override void CreateChildControls()
{
TB= new TextBox()
Controls.Add(TB);
}
}
但是,如果我现在使用以下控件测试 Web 表单:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<bleh:MyTextBox ID="MyTextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" />
然后在文本框中输入文本并提交后,TextBox1 中的文本在 Page_Load 中可用,而 MyTextBox1.Text 为空。我应该更改什么才能在发送回发时使其内容可用?
编辑:
我现在想到的是在初始化控件时简单地检查请求表单:
protected override void OnInit(EventArgs e)
{
TB.ID = this.ID + "_TextBox1";
Controls.Add(TB);
if (Page.Request.Form[TB.UniqueID] != null)
{
Text = Page.Request.Form[TB.UniqueID];
}
}
它似乎有效,但感觉有点hacky。这看起来是一个合理的方式吗?
I'm trying to create a few custom web controls wrapping some existing controls. One of them, for instance, is a wrap for a textbox (because I need additional behaviors such as corresponding validators). The problem is, I'm not sure how to get the data of that control to be sent in the postback.
A very simplified example:
public class MyTestBox: WebControl
{
protected TB { get; set; }
public Text
{
get { return TB.Text; }
set { TB.Text = value; }
}
protected override void CreateChildControls()
{
TB= new TextBox()
Controls.Add(TB);
}
}
But if I now test a webform with the control:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<bleh:MyTextBox ID="MyTextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" />
Then after entering text into the textboxes and submitting, the text in TextBox1 is available in Page_Load, while MyTextBox1.Text is empty. What should I change to have its content available when sending a postback?
Edit:
Something I thought of now is to simply check the request form when initializing the control:
protected override void OnInit(EventArgs e)
{
TB.ID = this.ID + "_TextBox1";
Controls.Add(TB);
if (Page.Request.Form[TB.UniqueID] != null)
{
Text = Page.Request.Form[TB.UniqueID];
}
}
It seems to be working, but feels sort of hacky. Does this seem like a reasonable way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该为动态生成的 TextBox 提供一个 ID,然后在控件的 Text 属性上,您应该找到该 TextBox 并返回其 Text。
You should give the dynamically generated TextBox an ID, then on the Text property of your control, you should find the TextBox and return its Text.
我没听清你的问题。
如果您需要向 Web 控件添加一些额外的行为,您应该执行与您想要扩展继承的类行为时相同的操作。
您可以直接继承 TextBox 类并添加您需要的所有额外行为
I was not getting your question.
If you need to add some extra behaviour to a web control you should do the same thing you would do in the case you want extend a class behaviour that is inheritance.
you can Inherits straigh form the TextBox class and add all the extra behaviour you need
它可能与页面生命周期有关。尝试在 Text 属性 getter 中使用
this.EnsureChildControls()
。有关页面生命周期的更多信息,请参阅 MSDN 文档。It could have something to do with the Page life cycle. Try to use
this.EnsureChildControls()
in the Text property getter. More on the Page life cycle can be found in MSDN documentation.