0
votes

I have a Custom.ascx file that is used on a number of pages. Custom.ascx contains a couple of controls and a button called cmdCustomPageButton.

When the user click on cmdCustomPageButton, cmdCustomPageButton executes a Protected Sub that gets some data from a database.

Page1.aspx that is using Custom.ascx has its own set of controls and procedures that it executes. It contains a button called cmdPage1Button and a procedure called RetriveData that is being called by other procedures as well within Page1.aspx.

When cmdPage1Button is clicked it calls RetriveData. RetriveData is only applicable to Page1.aspx. Page2.aspx and Page3.aspx both has a procedure similar to RetriveData but is only relevant to its own page.

Try to explain using code
Custom.ascx

Public Class Custom
 Protected Sub cmdCustomPageButton_Click(Byval sender as Object, ByVal e as EventArgs) Handels cmdCustomPageButton_Click
     //Code that gets data from the database
End Class

Page1.aspx

Public Class Page1
 Protected Sub cmdPage1Button_Click(Byval sender as Object, ByVal e as EventArgs) Handels cmdPage1Button_Click_Click
    //Some code    
    RetriveData()
 End Sub

 Sub RetriveData()
    //Some code
 End Sub

End Class

The question.
How do I call the different RetriveData procedure form the relevant pages being it Page1, Page2 or Page3 when cmdCustomPageButton is clicked ??

2

2 Answers

0
votes

Please refer this link below which has a better idea to a query similar to yours.

Calling a method in parent page from user control

In short, create a event delegate in your user control and handle the event in each of your page where the user control is used. Incase if the button in user control is clicked, the event will be fired in the respective parent page and you can call the RetriveData method in that event. Sorry, if your query was misunderstood.

0
votes

The MyUserControl.ascx page code.

Public Class MyUserControl
Inherits System.Web.UI.UserControl

Public Event UserControlButtonClicked As EventHandler

Private Sub OnUserControlButtonClick()        
      RaiseEvent UserControlButtonClicked(Me, EventArgs.Empty)
End Sub

Protected Sub TheButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' .... do stuff then fire off the event
    OnUserControlButtonClick
End Sub
End Class

The Default.aspx page code

Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    ' hook up event handler for exposed user control event
    AddHandler MyUserControl.UserControlButtonClicked, AddressOf Me.MyUserControl_UserControlButtonClicked
End Sub

Private Sub MyUserControl_UserControlButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
    ' ... do something when event is fired
End Sub
End Class

All credit gos to clklachu for pointing me in the right direction.

Conversion done by the following website link

Thanks