Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Accessing usercontrol elements

Author  Topic 

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-02-09 : 16:33:00
Hi All

I have a usercontrol and many webcontrols inside it
How can i access those subcontrols from my aspx page code behind
For example
I have a linkbutton in my user control
how can i get its click event from the aspx page code behind?

Can we do that?
It will be realy greatful if someone can give some ideas

thanks

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-02-10 : 02:16:08
Yes you can do that, but can you please explain a little more..

Are you simply saying you want the aspx page to do something when a button is clicked in the user control?

Just add a handler onto the button from the aspx page as long as you declared the control a Friend with events, but it's rare that you would ever need to pass this event back to the aspx page versus just handling it at the control level. There are numourus ways to accomplish what you are saying, but please clarify exactly what you are looking to do and hopfully I can tell you in my opinion what I feel is the bast way.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-02-10 : 14:08:33
A couple of ways to do this:

1) Event bubbling (see: http://aspnet.4guysfromrolla.com/articles/051105-1.aspx )

or

2) In your user control, declare the linkbutton as public (or add a public property to access it) and you should be able to attach events to it from your main web page.

or

3) create an appropriate event on your user control (i.e., "InvoiceDeleteLinkClick"), add an event handler to the linkbutton within the code of your user control, and then raise the user control event when the linkbutton is clicked.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-02-11 : 21:42:52
Thanks for the reply
Yes exaclty, when the link button in my user control is clicked, i want to change the label text in my aspx page.

Is there anything you can suggest more?
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-02-12 : 08:21:36
quote:
Originally posted by reflex2dotnet

Thanks for the reply
Yes exaclty, when the link button in my user control is clicked, i want to change the label text in my aspx page.

Is there anything you can suggest more?




I already suggested 3 ways to do it ... do you know how implement any of those options that I gave ?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-02-12 : 10:02:46
Yes, of course I am trying to do your way 2
But little bit confused

I declared the linkbutton as public property in user control
But confused how to use it in my aspx page

Public ReadOnly Property Link() As LinkButton
Get
Return lbtnDay
End Get

End Property
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-02-12 : 11:27:42
OK, if the ASPX page can now access the user control's linkbutton, on your ASPX page just declare a variable that you will use to attach your events to:

Protected WithEvents submitButton as LinkButton

Attach event code to this object variable as usual using the IDE or however you normally do it.

Finally, on the page's Load() or Init() event, we need to set this variable to reference the linkbutton on the user control. If we don't it is not set to an instance of an object anywhere and it won't do anything. So, if your usercontrol is called uc1 and the linkbutton property is called link1, we have something like:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
submitButton = uc1.link1
End Sub

Now you will be able to catch events thrown by the link button on your user control.

It is still better to create a custom event for your user control rather than referencing your user control's member and hooking up to their events, but this will technically work for you.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-02-12 : 11:58:22
Thanks.
It is working
Go to Top of Page
   

- Advertisement -