Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
conckrish@gmail.com - 27 Dec 2005 09:39 GMT
Hi all

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

Thanx
Krish.
Seraph Jiang - 27 Dec 2005 10:34 GMT
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);
 }

> Hi all
>
[quoted text clipped - 3 lines]
> Thanx
> Krish.
conckrish@gmail.com - 28 Dec 2005 04:58 GMT
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.
Bruce Wood - 28 Dec 2005 05:22 GMT
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.
conckrish@gmail.com - 28 Dec 2005 06:53 GMT
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??
Bruce Wood - 28 Dec 2005 23:51 GMT
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 ...
 }
conckrish@gmail.com - 28 Dec 2005 06:54 GMT
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??

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.