简体   繁体   English

提交不会在自定义WebControl(服务器控件)中触发

[英]Submit doesn't fire in custom WebControl (Server Control)

The submit button I create doesn't fire, not even if i set a breakpoint in Visual Studio trying to debug it. 我创建的提交按钮不会触发,即使我在Visual Studio中设置一个断点尝试对其进行调试也不会触发。

public class CustomControl : WebControl
{
    protected override void Render(HtmlTextWriter writer)
    {
        // Submit
        var submitButton = new Button{Text = "Submit", ID = "SubmitButton"};
        submitButton.Click += new EventHandler(SubmitClick);
        Controls.Add(submitButton);

        base.Render(writer);
    }

    protected void SubmitClick(object sender, EventArgs e)
    {
        Controls.Add(new LiteralControl("submited"));
        OnSubmit(EventArgs.Empty);
    }
}

It's added like this to the page 像这样添加到页面

<form id="form1" runat="server">
    <MyControls:CustomControl runat="server" />
</form>

Or maybe I'm just having the wrong approach here. 也许我只是在这里采用了错误的方法。

Add your button to the control inside the Page_Init or Page_Load event of the custom control instead of doing it in Render event. 将按钮添加到自定义控件的Page_InitPage_Load事件内的控件中,而不是在Render事件中进行操作。

Because after post back button click event would be called before the Render event. 因为在发布后退按钮之后,将在Render事件之前调用click事件。

Implement INamingContainer to activate EventHandlers 实现INamingContainer以激活EventHandlers

http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.inamingcontainer.aspx

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

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