自定义 Web 控制和回发

发布于 2024-10-19 16:11:06 字数 1046 浏览 4 评论 0原文

我正在尝试创建一些包装一些现有控件的自定义 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 技术交流群。

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

发布评论

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

评论(3

长伴 2024-10-26 16:11:06

您应该为动态生成的 TextBox 提供一个 ID,然后在控件的 Text 属性上,您应该找到该 TextBox 并返回其 Text。

public class BksTextBox: WebControl
{
protected TB { get; set; }
public Text
{
    get 
    { 
       return ((TextBox)this.FindControl("txt")).Text;
    }
}
protected override void CreateChildControls()
{
TB= new TextBox()
TB.ID = "txt";
Controls.Add(TB);
}
}

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.

public class BksTextBox: WebControl
{
protected TB { get; set; }
public Text
{
    get 
    { 
       return ((TextBox)this.FindControl("txt")).Text;
    }
}
protected override void CreateChildControls()
{
TB= new TextBox()
TB.ID = "txt";
Controls.Add(TB);
}
}
眼藏柔 2024-10-26 16:11:06

我没听清你的问题。
如果您需要向 Web 控件添加一些额外的行为,您应该执行与您想要扩展继承的类行为时相同的操作。
您可以直接继承 TextBox 类并添加您需要的所有额外行为

 public class BksTextBox: TextBox
  {
    //it is just as example
    public string TextToUpperCase
    {
    get
      {
       return Text.ToUpperCase();
       }
     }

}

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

 public class BksTextBox: TextBox
  {
    //it is just as example
    public string TextToUpperCase
    {
    get
      {
       return Text.ToUpperCase();
       }
     }

}
怎会甘心 2024-10-26 16:11:06

它可能与页面生命周期有关。尝试在 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.

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