Calling methods inside a user control from the container control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have an aspx page page with some existing controls code etc. This page has
an update button which updates a SQL Db based on any user changes. I need to
add a new section to this page and a few other pages. I think that it's best
to create a user control for this purpose. This user control will contain a
number of webcontrols e.g textbox, dropdown etc When the user clicks update
on the main page I need to update the DB as before for the original controls
plus the new ones from the user control.

What is the best way to do this? I thought that the user control should have
its own methods to take care of the updates but how should the main page call
these methods or is there a better way to do this?
 
The best way, in my opinion, is to have the page raise an event, which the
user controls are hooked into.

Basically, your page defines an event:
Public Event Save As EventHandler

on the click event of the page, you raise the event:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseEvent Save(Me, e)
End Sub

in the user control(s) you hook into the event:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler CType(Page, TYPEOFPAGEHERE).Save, AddressOf OnSave
'TYPEOFPAGEHERE is going to be like WebForm1 or something
End Sub

and then you have your control's save function:

Private Sub OnSave(ByVal source As Object, ByVal e As EventArgs)
'do your magic here
End Sub

Karl
 
Back
Top