Redundancy Methods in User Control

Hi I have a custom control that contains a button. I want to over ride a custom function when this button is pressed, for example

 Protected Sub btnUploadSpreadSheet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUploadSpreadSheet.Click
  MyMethod()
End Sub

  Public Overridable Sub MyMethod()
    ' this i want to over ride
    End Sub

      

and in my page where I added my control when I tried to drive

Protected Overrides Sub MyMethod ()

End Sub

      

It doesn't find this sub in the base class.

+3


source to share


3 answers


This is because the page is not a child of yours UserControl

(your page does not inherit from it) and this is the wrong approach anyway. Have a look at "Inheritance in Visual Basic": http://msdn.microsoft.com/en-us/library/5x4yd9d5%28v=vs.90%29.aspx

What you seem to want is to handle a custom UserControl event on your page:

Your UserControl:

Class ExcelSheetUploader
    Inherits UserControl
    Public Event Uploaded(ctrl As ExcelSheetUploader)

    Protected Sub btnUploadSpreadSheet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUploadSpreadSheet.Click
        RaiseEvent Uploaded(Me)
    End Sub
End Class

      



You can now handle it in your page:

Protected Sub ExcelSheetUploader_Uploaded(ctrl As ExcelSheetUploader) Handles ExcelSheetUploader1.Uploaded
    ' Do something .... '
End Sub

      

MSDN: RaiseEvent

+2


source


You cannot do this because your page is not extending the custom control. Only in such a situation will your implementation be suitable



+1


source


You cannot directly override methods or properties unless some class (such as a page) inherits the custom class (control). If you want to execute some functions on some event on the page or click a button then create public methods in UserControl that raise some event or execute some code.

Note . It is necessary to monitor the life cycle of the page.

Check out this little piece of code that might be helpful to understand.

Private Class MyControl
    Inherits NavronChartControl.UserControls.MyControlBase

    Protected Overrides Sub OnChartTypeChanged(chartType As UserControls.ChartType)
        MyBase.OnChartTypeChanged(chartType)
    End Sub

///Call this OnChartTypeChanged(...) using some method 
/// Or Create your public methods for your functionality


End Class

      

/// Method in parent control

'OnChartTypeChanged is method that raise the event with argument

Protected Overridable Sub OnChartTypeChanged(chartType As ChartType)
    'ChartTypeChang is an Event
    RaiseEvent ChartTypeChanged(chartType)
End Sub

      

Check out ASP.NET Programming - Custom and Custom Controls Event Handling in the VB.NET section .

MSDN - Raising an Event - You can create your own custom event argument that contains some data that you want to pass through the event. ege.X

0


source







All Articles