How to add click event for dynamically created User control page?

C

conckrish

Hi all

Can anyone tell me how to add a click event for a user control page
dynamically???


Thanx
Krish.
 
S

Seraph Jiang

I use below code to add a dynamically control and += an event for it
override the OnInit event

protected override void OnInit(EventArgs e)
{
btnGo=new Button();
btnGo.ID="btnGo";
btnGo.Text="Go";
btnGo.CssClass="Button";
this.Controls.Add(btnGo);

this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
base.OnInit (e);
}
 
C

conckrish

Dear Seraph Jiang ,

I should not create and use the button in my
appln.If i click anywhere in the User control ,it ll do something.The
user control contains 4 labels only... Now plz tell me is any other way
(without button creation) to do this task??

Thanx
Krish.
 
B

Bruce Wood

So, if I understand you, you want to respond to the "Click" event of
the UserControl itself.

If that's so, Seraph just showed you how to do it for a dynamically
created button. Just do exactly the same thing, substituting your
UserControl for Seraph's Button.

If I misunderstand, then maybe you could post the code to your
UserControl and explain in more detail what it is you want to do.
 
C

conckrish

Dera Bruce,

Yes, I want to respond to the Click event of the UserControl
itself.If I click on the User Control it ll do some action...Plz tell
me How to do??
 
C

conckrish

Dear Bruce,

Yes, I want to respond to the Click event of the UserControl
itself.If I click on the User Control it ll do some action...Plz tell
me How to do??
 
B

Bruce Wood

Well, just do exactly what Seraph showed you, but with your
UserControl:

MyUserControl user = new MyUserControl();
user.ID="user";
this.Controls.Add(user);


user.Click += new System.EventHandler(this.user_Click);

and then, somewhere in your code:

private void user_Click(object sender, System.EventArgs e)
{
... do whatever you want when the butotn is clicked ...
}
 

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

Top