简体   繁体   中英

Button Event Not Firing in WebControl

I'm attempting to create a "server control" that derives from WebControl. This needs to be done entirely in C# so that it can be compiled into a .dll. I am not interested in creating a user control with a .ascx file (I actually already have a "user control" but I want to add it to a library so I'm converting it).

My control is dead simple right now and I can't get a button event to fire:

public class ButtonWrapper : WebControl
{
    protected Button button;

    public event EventHandler<Events.GenericEventArgs<Tuple<int, string>>> ButtonClicked;

    public void Button_Click(object o, EventArgs e)
    {
       ButtonClicked(this, new Tuple<int,string)(0, "abc"));
    }

    public void WHERE_DO_I_PUT_THIS_CODE()
    {
        button = new Button()
        {
          ID = "button",
          Text = "Button"
        };
        button.Click += new EventHandler(Button_Click);
    }
}

Where do I need to "create" the button? I currently have it in an overloaded CreateChildControls():

protected override void CreateChildControls()
{
    this.Controls.Clear();

    button = new Button()
    {
          ID = "button",
          Text = "Button"
    };
    button.Click += new EventHandler(Button_Click);

    this.Controls.Add(button);
    this.ChildControlsCreated = true;
 }

The control loads just fine, but when I click on the button, the page just refreshes and the event is never fired. I want to the button in ButtonWrapper to fire so that the parent page can listen. I think I'm close but I'm missing something simple.

Edit: I don't quite have the right event variables types for EventArgs passing and such when I was simplifying the problem for this question.

I created new solution and i tried it. You can download from this link : http://dosya.co/download.php?id=51BACA121 I m at work and most of upload sites blocked. So i couldnt upload another site. If u cant download i can try find different upload site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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