Forgive me for seeming to hijack this older thread, but I believe the topic which was covered (or attempted) here is close to my problem.
I have MasterPage which includes a UserControl. However, most of the "action" to this UserControl comes from the page "contained in" the MasterPage. (In other words, a triggering action is invoked on the main part of the page which needs to be reflected
in a displayed UserControl within the MasterPage header, in my case.)
My MasterPage is MasterAlt.aspx (with its corresponding ".aspx.vb"). In the VB file, I have:
Public Sub AddItemToCart(ByVal pItem As ListItem)
ShopCart1.ListItem = pItem
End Sub
In the UserControl "ShopCart.aspx.vb" I have:
Public Property ListItem() As ListItem
Get
Return (_ListItem)
End Get
Set(ByVal value As ListItem)
_enabled = True
pnlShopCart.Visible = _enabled
lstShoppingCart.Items.Insert(0, New ListItem(value.Text, value.Value))
End Set
End Property
In a typical page that wants to post an item to this 'cart' I have:
Private aMP As MasterPage
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then' FIRST-TIME initialization ...
Session("PageLastUpd") = CONST_LAST_UPDATE_DATE_TIME
aMP = CType(Me.Master, MasterAlt)
End If
Which *should* establish a page-level item which can then be used in the button controller for when a person adds an item to the cart:
Protected Sub btnCart1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCart1.Click
Dim myNew As New ListItem
myNew.Text = "(something)"
myNew.Value = 2 ' item number
aMP.AddItemToCart(myNew)
*************
I'm getting the error notation "Error 1 'AddItemToCart' is not a member of 'System.Web.UI.MasterPage'." on what is line 8 of the code immediately above.
WHERE AM I GOING WRONG? (I searched 4 separate ASP.NET books in an attempt to find a discussion of "scope" which this problem plainly seems to involve, with no luck.)
Any hints, guidance gratefully accepted and appreciated! :) mahalo ... KevInKauai
Your problem is that you're only casting aMP on a non postback, btnCart1_Click is a postback!..
I would recommend you to create a property to access your "casted" master page, i'm not a VB developer but here is how the C# equivalent would look like:
thyphoon1
Member
1 Points
5 Posts
Call a function in a MasterPage from the child page
Sep 27, 2007 03:09 PM|LINK
Hi,
I have a function in my master page and i want to call it from a "child" page....
What i must to do ?
thanks
ps2goat
Star
10845 Points
1977 Posts
Re: Call a function in a MasterPage from the child page
Sep 27, 2007 04:14 PM|LINK
Make sure the function is accessible to the page (i.e. declared public), and use the MasterType declaration in the ContentPage:
In the page, use Page.Master.MyFunction() to access the function.
*Note: before being able to access the function, you'll need to save & build.
MCP - Web Based Client Development .NET 2.0
thyphoon1
Member
1 Points
5 Posts
Re: Call a function in a MasterPage from the child page
Sep 27, 2007 04:17 PM|LINK
right ;-)
scott@elband...
Star
11346 Points
1865 Posts
Re: Call a function in a MasterPage from the child page
Sep 27, 2007 04:19 PM|LINK
Code on masterpage, master page is called MyMasterPage:
Public Sub dothis()Response.Write("bgbb")
End Sub
Code on Child page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim aMP As MyMasterPage = CType(Me.Master, MyMasterPage)
aMP.dothis() End Sub
docluv
Star
12685 Points
2005 Posts
ASPInsiders
MVP
Re: Call a function in a MasterPage from the child page
Sep 27, 2007 06:09 PM|LINK
This is one of my Blog entries on how to bind to the events of a Master page in the child page.
http://professionalaspnet.com/archive/2007/01/11/Handling-Master-Page-Click-Events-in-the-Content-Page.aspx
master pages
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Call a function in a MasterPage from the child page
Oct 01, 2007 03:07 AM|LINK
Hi
You can refer scorpioafrica's post on this thread: http://forums.asp.net/p/1157414/1904247.aspx#1904247
Hope it helps.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
KevInKauai
Member
233 Points
301 Posts
Re: Call a function in a MasterPage from the child page
Oct 11, 2008 12:36 AM|LINK
Aloha Chris and Amanda - -
Forgive me for seeming to hijack this older thread, but I believe the topic which was covered (or attempted) here is close to my problem.
I have MasterPage which includes a UserControl. However, most of the "action" to this UserControl comes from the page "contained in" the MasterPage. (In other words, a triggering action is invoked on the main part of the page which needs to be reflected in a displayed UserControl within the MasterPage header, in my case.)
My MasterPage is MasterAlt.aspx (with its corresponding ".aspx.vb"). In the VB file, I have:
Public Sub AddItemToCart(ByVal pItem As ListItem) ShopCart1.ListItem = pItem End SubIn the UserControl "ShopCart.aspx.vb" I have:In a typical page that wants to post an item to this 'cart' I have:
Which *should* establish a page-level item which can then be used in the button controller for when a person adds an item to the cart:masterPage user control Page
KevInKauai
Member
233 Points
301 Posts
Re: Call a function in a MasterPage from the child page
Oct 12, 2008 09:43 PM|LINK
^BUMP^
Please -- a hint, point me to a manual or chapter.
tia ... KevInKauai
pnmcosta
Member
145 Points
61 Posts
Re: Call a function in a MasterPage from the child page
Oct 12, 2008 10:47 PM|LINK
Hi Kev,
Your problem is that you're only casting aMP on a non postback, btnCart1_Click is a postback!..
I would recommend you to create a property to access your "casted" master page, i'm not a VB developer but here is how the C# equivalent would look like:
protected MasterAlt MyMasterPage{ get{ return (MasterAlt)this.Master; } }That way you don't need to worry about declaring and casting a variable..