Add an eventhandler on a button_click

G

Guest

Hi,
I am having this problem:
I have developed a composite component which has 2 components: a textbox and a button.
I need to add an eventhandler to a button click. I have added the eventhandler, in the CreatechildControls override, but it doesn't work.
this is the code:

namespace Space1
{
public class LookUp: System.Web.UI.WebControls.WebControl , INamingContainer
{
protected System.Web.UI.WebControls.TextBox txtRisultato = new TextBox();
protected System.Web.UI.WebControls.Button BtnLookUp = new Button();

public LookUp()
{
EnsureChildControls() ;
}
private void BtnLookUp_Click(Object sender, EventArgs e)
{
txtRisultato.Text = "Hi!!!";
}
protected override void CreateChildControls ()
{
Controls.Add(BtnLookUp);
Controls.Add(txtRisultato);
BtnLookUp.Text = "...";
Controls.Add (new LiteralControl ("<TABLE>"));
Controls.Add(new LiteralControl("<TR>"));
Controls.Add(new LiteralControl("<TD>"));
Controls.Add(txtRisultato);
Controls.Add(new LiteralControl("</TD>"));
Controls.Add(new LiteralControl("<TD>"));
Controls.Add(BtnLookUp);
Controls.Add(new LiteralControl("</TD>"));
Controls.Add(new LiteralControl("</TR>"));
Controls.Add (new LiteralControl("</TABLE>"));
BtnLookUp.Click+=new EventHandler(BtnLookUp_Click);
}
}
}

How can I fire the button click event?

Thank you
Alessandro Rossi
 
C

Chris Jackson

The problem is this: you are instantiating this control when you create an
instance of the class, which is every time that you respond to a request.
After you instantiate this control, you then add an event handler to it. Of
course, there is no event to respond to on this newly instantiated control -
the event happened on the control that it instantiated on the first page
request.

What you need to do, in the OnInit method, is make a call to
Page.RegisterRequiresPostback(this). To ensure that you actually have a
hosting page, you should check for Page != null first. Then, you can
implement IPostBackDataHandler to handle the post back data. You have two
methods to implement: LoadPostData and RaisePostDataChangedEvent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top