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
Related
Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.ID = "but"
AddHandler but.Click, AddressOf Button_Click
Me.form1.Controls.Add(but)
End Sub
The event control for this is as below.
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Im getting the error
Multiple controls with the same ID '1' were found
The subroutine createbutton works on page load as follows.
Public Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
createbutton()
End Sub
Help is appreciated , Thanks :)
i think you are a guy who worked a lot with VB 6.0
Control array feature is available in VB 6.0
in VB.net everything is depend upon control ID.
if you don't have specific requirement like ID should be same then i suggest pls append prefix and incremental ID would resolve your issue.
Let me know if you have some specific requirements
Thanks
Asif
Corrected Code, Instead of ID it's name which allow uniqueness
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createbutton()
End Sub
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.Name = "but"
AddHandler but.Click, AddressOf Button_Click
Me.Controls.Add(but)
End Sub
End Class
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.
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
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
From the outside of a control, is it possible to find out what stage of the Page LifeCycle (Init, Load, PreRender etc), a particular control or page is up to?
For example, in pseudo code:
if myControl.CurrentLifeCycle == Lifecycle.Init
{ do something }
I'm afraid there is no builtin function to check in what Page-Lifecycle phase a Page is.
It is also difficult to add this functionality without handling all events in the Page itself, because some events are protected. Therefore you could also inherit the LifeCycleListener-class from Control, add it in the constructor to the page listening and override all events.
If you only need the public "phases" PreInit,Init,Load,DataBinding,PreRender,Unload,Disposed have a look at following approach(VB.Net, but i think you'll get the idea):
Public Enum LifeCyclePhase
AfterPreInit
AfterInit
AfterLoad
AfterDataBinding
AfterPreRender
AfterUnload
AfterDisposed
End Enum
Public Interface ITrackingLifeCycle
ReadOnly Property GetLifeCycleListener() As LifeCycleListener
End Interface
Public Class LifeCycleListener
Public Sub New(ByVal ctrl As Control)
Me._PageListening = ctrl.Page
AddListener()
End Sub
Private _CurrentPhase As LifeCyclePhase
Private _PageListening As Page
Public ReadOnly Property CurrentPhase() As LifeCyclePhase
Get
Return _CurrentPhase
End Get
End Property
Public ReadOnly Property PageListening() As Page
Get
Return _PageListening
End Get
End Property
Private Sub AddListener()
AddHandler _PageListening.PreInit, AddressOf PreInit
AddHandler _PageListening.Init, AddressOf Init
AddHandler _PageListening.Load, AddressOf Load
AddHandler _PageListening.DataBinding, AddressOf DataBinding
AddHandler _PageListening.PreRender, AddressOf PreRender
AddHandler _PageListening.Unload, AddressOf Unload
AddHandler _PageListening.Disposed, AddressOf Disposed
End Sub
Private Sub PreInit(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterPreInit
End Sub
Private Sub Init(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterInit
End Sub
Private Sub Load(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterLoad
End Sub
Private Sub DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterDataBinding
End Sub
Private Sub PreRender(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterPreRender
End Sub
Private Sub Unload(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterUnload
End Sub
Private Sub Disposed(ByVal sender As Object, ByVal e As EventArgs)
Me._CurrentPhase = LifeCyclePhase.AfterDisposed
End Sub
End Class
The handler in this class are called after the handler in the page itself, so if you f.e. check the CurrentPhase in Page.Init you'll get PreInit. Therefor i have called this phase AfterPreInit.
Partial Public Class _Default
Inherits System.Web.UI.Page
Implements ITrackingLifeCycle
Private lcl As New LifeCycleListener(Me)
Public ReadOnly Property GetLifeCycleListener() As LifeCycleListener Implements ITrackingLifeCycle.GetLifeCycleListener
Get
Return lcl
End Get
End Property
You can now check the lifecycle-phase everywhere, even without a reference to a control via HttpContext.Current:
Public Class FooClass
Public Shared Sub Foo()
If Not (HttpContext.Current Is Nothing OrElse HttpContext.Current.Handler Is Nothing) Then
If TypeOf HttpContext.Current.CurrentHandler Is ITrackingLifeCycle Then
Dim page As ITrackingLifeCycle = DirectCast(HttpContext.Current.CurrentHandler, ITrackingLifeCycle)
Dim phase As LifeCyclePhase = page.GetLifeCycleListener.CurrentPhase
End If
End If
End Sub
End Class
This is neither tested sufficiently nor used by myself and certainly improvable, but maybe it helps you in your current situation.
I think what you try to achieve is conceptually wrong because you are thinking at the page events as page state. The page can’t be at “OnInit/OnLoad/…” state just because it’s an event.
What do you need it for? maybe we could suggest you a better approach to achieve your goal.
The „outside of your control” is the „inside” of another control or page or something being in a linked state of the lifecycle. You know its state already without testing.
I found out there is actually an internal property of the Page class that does just what I am looking for: it is an enum called ControlState:
internal enum ControlState
{
Constructed,
FrameworkInitialized,
ChildrenInitialized,
Initialized,
ViewStateLoaded,
Loaded,
PreRendered
}
I believe it is possible to access internal members in C#4, see here
As far as I understand the page lifecycle, you can't do it. Basically, Page class is the guy that has to raise events in a specific order. There is nothing in built that will tell the stage of processing. But in order to something, you can create a property and set up this property in different stages of processing.