Hi i have a user control that contains a button. I want to Over ride a custom function on click of that button like
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 have added my control when i tried to over ride
Protected Overrides Sub MyMethod ()
End Sub
It is not finding that sub in the base class.
That's because the page is not a child of your UserControl(your page does not inherit from it) and this is the wrong approach anyway. Look up "Inheritance in Visual Basic": http://msdn.microsoft.com/en-us/library/5x4yd9d5%28v=vs.90%29.aspx
What you apparently want is to handle a custom UserControl's event in 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
Now you can handle it in your page:
Protected Sub ExcelSheetUploader_Uploaded(ctrl As ExcelSheetUploader) Handles ExcelSheetUploader1.Uploaded
' Do something .... '
End Sub
MSDN: RaiseEvent
You cannot do this because your page is not extending the user control. Only in such a situation your implementation would be suitable
You can not directly override the methods or properties if some class
(e.g. Page) does not inherit You custom Class(Control). If you want to
do some functionality on some event on the page or button click then
create the public methods on the UserControl that raise some event or
execute some code.
Note - Must follow the page - life cycle.
Check this little code snippet that may 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 at 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 the Programming ASP.NET- Custom and User Controls 's Handling events in VB.NET section.
MSDN - Raising an Event - you can create your own custom event argument that hold some data that you want to pass through event. e.g. e.X
Related
I have a server control that I am trying to get to save properties as control states but for some reason the properties are not persisting across partial postbacks.
The psuedo code is as follows:
Public Class FileUpload
Inherits ScriptControl
Implements INamingContainer, IPostBackEventHandler
Public Property newFileExt() As String
Get
Dim foundList As String = DirectCast(ViewState(Me.UniqueID & "_fileExt"), String)
If foundList IsNot Nothing Then
Return foundList
Else
Return String.Empty
End If
End Get
Set(ByVal value As String)
ViewState(Me.UniqueID & "_fileExt") = value
End Set
End Property
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
Page.RegisterRequiresControlState(Me)
End Sub
Protected Overrides Function SaveControlState() As Object
Dim controlState(6) As Object
controlState(0) = MyBase.SaveControlState()
controlState(1) = newFileExt
Return controlState
End Function
Protected Overrides Sub LoadControlState(ByVal savedState As Object)
Dim controlState() As Object
controlState = CType(savedState, Object)
MyBase.LoadControlState(controlState(0))
newFileExt = CType(controlState(1), String)
End Sub
end class
On this control is an asyncFileUpload ajaxcontroltoolkit control and a button. I have an event for upload complete:
Protected Sub SaveUploadedFile(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Handles asyncFileUpload.UploadedComplete
newFileExt= "Some Value"
end sub
Protected Sub bntSelectResults_click(ByVal sender As Object, ByVal e As EventArgs) Handles bntSelectResults.Click
If (newFileExt= "") Then
'this always returns as empty
End If
end sub
So, UploadedComplete is complete it should set the controls state. then, when the user click the button it should read it. Through debugging, I can see that it is set correctly in UploadedComplete event but null when read. Is this due to the cycle of the page or something?
Thanks
jason
EDIT
I traced out the path for how the page cycle is running:
User clicks the async file upload control's browse button and selects a file. This causes the upload process to start
a. OnInit gets called
b. LoadControlState gets called
c. OnLoad gets called
d. asyncFileUpload.UploadedComplete gets called and I set the newFileExt property
here.
e. SaveControlState gets called. newFileExt is set here properly
User clicks a button on the control that initiates another partial postback/update of the update panel
a. OnInit gets called
b. LoadControlState gets called. I can see that the newFileExt property is not set
c. OnLoad gets called
d. Buttons click event gets called and the property is read (which is no longer set)
e. SaveControlState gets called and cycle ends
So, as best as I can tell, the asyncFileUpload application has issues with ViewStates/ControlStates. I ended up just just using sessions.
The following method is return an error: 'Event init cannot be found' and get error on System.Web.UI.Page
Visual Studio 2010, framework 3.5
Public Class_default
Inherits System.Web.UI.Page
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
End Sub
I am not VB.NET expert (I am writing on C#) but i can say that the cause of the problem is that you need to override OnInit method instead of Page_Init.
So, you need to use the following code:
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Friend Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
End Sub
End Class
I think Maxim is correct, however you can also add your event handler by doing:
AddHandler Me.Init, AddressOf Page_Init
I think that's right, sorry my VB is pretty rusty.
Well,
I assume you are using AutoEventWireup="true" in your markup but you have also marked your handler with the Handles keyword.
AutoEventWireup won't work, its not able to access private members of your class directly.
Set AutoEventWireup="false" and save its needless performance penalty, the handler should still fire because it explicitly Handles the Page.Init event.
Since you're handling the event explicitly you are also free to change the name of the event handler and remove the ugly _.
The majority of your Crystal Reports code is likely in the Page_Load event. If you move it to the Page_Init you'll find it works correctly.
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 ??
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.
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
How can I call the Sub on the content page?
On the content page, there is let's say this:
Public Sub MySub()
End Sub
On the master page I have this:
Dim cph As ContentPlaceHolder = CType(Page.Form.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
Why do you need this? You can't be sure to have a particular contentpage. A Masterpage's purpose is re-usability and therefore many pages should use it. Why you need to access a special page?
What you could do is, add an event to the Masterpage, raise it when necessary and handle it in the ContentPage. For example...
in Master:
Partial Public Class ERPMaster
Inherits System.Web.UI.MasterPage
Public Event MasterLoaded(ByVal master As MasterPage)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RaiseEvent MasterLoaded(Me)
End Sub
In Content:
Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
AddHandler DirectCast(Master, ERPMaster).MasterLoaded, AddressOf MasterLoaded
End Sub
Private Sub MasterLoaded(ByVal master As MasterPage)
MySub()
End Sub
Public Sub MySub()
End Sub
I assume that you are using ASP.NET?
Do you have a MasterType directive at the top of the content page file? If so, you can simply call functions on the master page using the following syntax:
Master.MySub()
The Master property of the content page is already typed to the page specified in the MasterType directive, so you can easily access any of the functions that it defines.
See MSDN for more information on interacting with master and client pages: http://msdn.microsoft.com/en-us/library/c8y19k6h(v=VS.100).aspx
I have the following BasePage class...
Public Class BasePage
Inherits System.Web.UI.Page
Private litError As Literal
Protected SO As Session
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SO = Session.Item("SO")
If SO Is Nothing Then
Session.Abandon()
Response.Redirect("~/timeout.htm")
End If
litError = Page.FindControl("litError")
If litError IsNot Nothing Then
litError.Visible = False
End If
End Sub
Protected Sub ShowMessage(ByVal Message As String)
Show(Message, "message")
End Sub
Protected Sub ShowError(ByVal Message As String)
Show(Message, "error message")
End Sub
Protected Sub ShowSuccess(ByVal Message As String)
Show(Message, "success message")
End Sub
Private Sub Show(ByVal Message As String, ByVal CssClass As String)
If litError IsNot Nothing Then
litError.Text = String.Format("<span class=""{0}"">{1}</span>", CssClass, HttpUtility.HtmlEncode(Message))
litError.Visible = True
End If
End Sub
End Class
Every page in this application inherits this class. The SO variable represents a custom session class, that is very simple and just holds a couple of basic settings to be used throughout the application. The problem is, my Page_Load in this base class does not fire if a natural postback occurs (in this case, it is a gridview postback by sorting/paging). Then later in my code when I reference SO, I get a null reference exception because it hasn't been pulled from session.
Why doesn't the base Page_Load fire?
Try moving your code into the Page_Init event.
Microsoft has some info on each event in the lifecycle http://msdn.microsoft.com/en-us/library/ms178472.aspx. This MSDN page tells you what types of things you should handle in each event.
You might want to think about implementing SO as a property, where the Get does (not sure if this is correct VB...)
Dim so As Session = Session.Item("SO")
If so Is Nothing Then
Session.Abandon()
Response.Redirect("~/timeout.htm")
End If
return so
It could be that something else is happening in the Init events that is causing it to fail. So rather than it not being called it just hasn't been called yet.
It could be that the autoevent wireup isn't wiring it up correctly, tend to override the OnInit event and attach the events manually myself, I have also read somewhere that this improves perfomance by not requiring the framework to do heaps of reflection on every post.
But back to your problem... try making the SO object private and create a property accessor for it that first checks that if the private is set, if not set it, before returning the private variable. If it isn't set and can't be found then it can abort the same way you are doing in the Load. This means that to load the variable you won't be dependent on the Page_Load from firing and thus the SO object should be available for you during the init routines, if you need it.