简体   繁体   English

Asp.NET服务器控件回发

[英]Asp.NET Server Control Postback

I have a Control I want to create. 我有一个想要创建的控件。 Here's a simple example of what I was to accomplish. 这是我要完成的一个简单示例。

I want the control to contain a button. 我希望控件包含一个按钮。

Button b = new Button();
b.Text = "Test";
b.Click += new EventHandler(b_Click);

this.Controls.Add(b);

Now, the control renders fine, the button shows up on the page. 现在,控件呈现正常,按钮显示在页面上。 The heart of the problem I'm having is that the b_Click Event Handler is never triggered. 我遇到的问题的核心是b_Click事件处理程序永远不会被触发。

protected void b_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

Any help here would be much appreciated. 这里的任何帮助将不胜感激。 I don't want to use a User Control here for purely selfish reasons and would like to totally encapsulate this in a single DLL. 我不想在这里使用用户控件纯粹是出于自私的原因,并希望将其完全封装在一个DLL中。

Thanks In Advance. 提前致谢。

EDIT** 编辑**

namespace ClassLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl
    {

        protected override void CreateChildControls()
        {
            Button b = new Button();
                    b.ID = "button";
            b.Text = "Click Me";
            b.Click += new EventHandler(b_Click);

            this.Controls.Add(b);

            base.CreateChildControls();
        }

        protected void b_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new LiteralControl("<p>Click!</p>"));
        }

    }
}

So from the comments I've tried this. 所以从评论中我试过这个。 The simplest of exampes, still no go. 最简单的例子,仍然没有去。 Is there something I'm fundamentally missing? 有什么东西我从根本上失踪了吗?

public class WebCustomControl1 : WebControl

needed to be 需要

public class WebCustomControl1 : WebControl, INamingContainer

that's it. 而已。 that's all that was needed to make this postback issue work. 这就是使这个回发问题发挥作用所需要的一切。

Override the CreateChildControls method and create/add the button (and register the handler) in that method. 覆盖CreateChildControls方法并在该方法中创建/添加按钮(并注册处理程序)。

Using OnInit/OnLoad to create controls like this is incorrect and will lead to inconsistent behavior (such as what you're experiencing). 使用OnInit / OnLoad创建这样的控件是不正确的,会导致行为不一致(例如您遇到的情况)。

Edit: You might also try setting button.ID so that it's the same on every postback, it's possible the raised event isn't seeing it for that reason. 编辑:您也可以尝试设置button.ID,以便在每次回发时都是相同的,由于这个原因,凸起的事件可能没有看到它。

Please show the definition of your control class. 请显示您的控件类的定义。

Does your control inherit from Control, WebControl, or CompositeControl. 您的控件是继承自Control,WebControl还是CompositeControl。 Try inheriting from CompositeControl and see if that helps. 尝试继承CompositeControl并查看是否有帮助。

So your button renders to the page correctly and causes a postback when the user clicks it, but the OnClick code never runs? 因此,您的按钮正确呈现到页面并在用户单击时导致回发,但OnClick代码永远不会运行?

Remember, whenever you do a postback you're working with a brand new instance of your page class. 请记住,无论何时进行回发,您都要使用页面类的全新实例。 Part of the postback process is running whatever event was triggered by the user. 回发过程的一部分正在运行用户触发的任何事件。 For this to happen, the asp.net processor needs to know about the event at a specific point in the page lifecycle. 为此,asp.net处理器需要了解页面生命周期中特定点的事件。 At this point, your button must already be on the page with the event handler registered . 此时,您的按钮必须已经在已注册事件处理程序的页面上 If this doesn't happen, your event won't fire. 如果没有发生这种情况,您的活动将不会触发。 Try moving your button creation up earlier in the page lifecycle. 尝试在页面生命周期的早期移动按钮创建。

This may be the case of assigning the handlers well after the event is raised. 这可能是在事件引发后很好地分配处理程序的情况。

My common practice when dealing with controls are: 处理控件时我的常见做法是:

  1. Use Control.Init event to initialize everything (default text, handlers, size, composition, etc.) and also initialize the child controls. 使用Control.Init事件初始化所有内容(默认文本,处理程序,大小,组合等),并初始化子控件。
  2. Use Control.Load event to handle any data binding operation (based on the properties/fields set during the Init event). 使用Control.Load事件来处理任何数据绑定操作(基于Init事件期间设置的属性/字段)。

Check the identifier of the button control before the postback and after the postback. 在回发之前和回发之后检查按钮控件的标识符。 If it's not the same, it won't work. 如果它不相同,它将无法工作。

You can overwrite the identifier yourself if needed. 如果需要,您可以自己覆盖标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM