Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

user control events

  Asked By: Cory    Date: Jun 09    Category: Asp.net    Views: 776
  

i have user control inside form.
inside the control i have button to postback to the server.
how can i raise event when the form in the container page is submitted
(and only then - not when there is server post back inside the control)

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Bellona Lopez     Answered On: Jun 09

I'm not totally sure I understand your question. Did you want to raise an event
when the button  was clicked on the UserControl?

 
Answer #2    Answered By: Jonathan Harrison     Answered On: Jun 09

i meant when the submit button  was clicked in the container  page.

 
Answer #3    Answered By: Gerald Cruz     Answered On: Jun 09

I had the same question last week. I received the Email below from Peter
Lanoie. It was a great help.

Note: Since your control  is already loaded into the form, you won't need the
first two lines of code in the Load event  of the page. I couldn't get the
function to work because of signature problems (still haven't figured this out)
- but when I replaced the function with a subroutine - everything worked well.
If you're having problems getting this to work after a bit - I'll send you my
sample that I was able to get working.
Let's consider this example (in VB):

We have an ASPX page  "myPage.aspx" (Class myPage) and a user  control

"myUserControl.ascx" (Class myUserControl). There is a function in the PAGE

(myPage.myPageFunction()) that wants to run when something happens in the

CONTROL. In the Control we need to define an event and a delegate for that

event. We can then raise this event from where ever we need within the

control, in your case it's the button  click routine.

User Control file:


Class myUserControl

...

Public Event MyEvent As MyEventDelegate

Public Delegate Sub MyEventDelegate(ByVal sender As System.Object, ByVal e

As System.EventArgs)

Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyButton.onClick

RaiseEvent MyEvent(Me, e)

End Sub

...

End Class

Now you have your event defined and raised. We need to tie this in to the

ASPX page that contains the control. Presumably, in page load we are

loading up the user control. Once we have an object for the user control,

we can hook a function in the PAGE into the event in the CONTROL with

"AddHandler".

ASPX Page:

Class myPage

...

Private Sub Page_Load(...)

...

Dim objMyUserControl As myUserControl

objMyUserControl = CType(LoadControl("myUserControl.ascx"),

myUserControl)

AddHandler objMyUserControl.MyEvent, AddressOf myPageFunction

...

End Sub

...

Private Function myPageFunction(ByVal sender As System.Object, ByVal e As

System.EventArgs)

'Do whatever you need to in here.

End Function

...

End Class

One important thing to remember: Any function that you hook into an event

with must have the same signature as the event delegate. That's why

"myPageFunction" has "sender" and "e" as arguments. The delegate will pass

those along in the event.

Now when you press the button in the user control, the event triggers the

function call in the page. You can extend this functionality and create a

function in ANOTHER user control, and hook events  in two user controls

together. You do this in a similar fashion as we did in the ASPX

page_load(). Instead of hooking a page function to a user control event,

you just hook one UC function to the other UC event:

Dim objMyFirstUC as MyFirstUC

Dim objMySecondUC as MySecondUC

objMyFirstUC = CType(LoadControl("myFirstUC.ascx"), MyFirstUC)

objMySecondUC = CType(LoadControl("mySecondUC.ascx"), MySecondUC)

AddHandler objMyFirstUC.FirstUCEvent, AddressOf

objMySecondUC.DoFirstUCEvent

AddHandler objMySecondUC.SecondUCEvent, AddressOf

objMyFirstUC.DoSecondUCEvent


As you can see, you can go on and on, tying together controls to the page,

or to other controls.

Check out this article. It's basically what I discussed.

http://www.dotnetextreme.com/code/eventhandling.asp

 
Answer #4    Answered By: Zoar Mizrachi     Answered On: Jun 09

just to make sure:
There is a function in the PAGE

(myPage.myPageFunction()) that wants to run when something happens in the

CONTROL.

i want to raise event  in the control  when the aspx is submitted.

 
Answer #5    Answered By: Eileen Carr     Answered On: Jun 09

You could access a function in the UserControl on the page  by making it public -
The is syntax for accessing the method is UserControl.MyUserControlFunction.

 
Didn't find what you were looking for? Find more on user control events Or get search suggestion and latest updates.




Tagged: