using master page controls in content page and adding click event

Hi

 I've currently got a control on my master page and i'm trying to write an even on the content page that fires when the button is clicked, I don't think it is working though.

Can someone help? The code is below:

 masterpage

<asp:ImageButton id="imgButton" runat="server"/>

code behind

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load, Me.Load

Public ReadOnly Property lnkbFromMasterPage() As ImageButton
Get
Return imgButton
End Get
End Property

 content page code behind:

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load, Me.Load
If Not IsPostBack Then

Dim cmp As MasterPage = CType(Master, MasterPage)
If Not IsNothing(cmp) Then
AddHandler cmp.lnkbFromMasterPage.Click, AddressOf lnkbFromMasterPage_Click
End If

End If
End Sub

Protected Sub lnkbFromMasterPage_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

Response.Write("test")

End Sub

I'm not getting the message printed to the screen so I presume it isn't working correctly can anyone help? Thanks in advance.

Gemma

0
supergirl_gem
5/12/2008 12:27:48 PM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

6 Replies
1455 Views

Similar Articles

[PageSpeed] 39
Get it on Google Play
Get it on Apple App Store

 First, you can do it this way.

Dim imgButton As ImageButton = cType(Master.FindControl("imgButton"), ImageButton)

  

Or..you can expose the control as a public property in the page load event of the Master Page.

 

Public ReadOnly Property lnkbFromMasterPage() As ImageButton
Get
Return imgButton
End Get
End Property

  Then, on your content page, you can access the control by doing...

Master.imgButton
  
Jeffrey Way | My Blog

I update my blog on a daily basis with views on web development. I always appreciate new readers.
0
jeff_way
5/12/2008 12:52:28 PM

You could also check out the following "How do I?" video on this site...

http://www.asp.net/learn/videos/video-266.aspx


Kelsey Thornton
(In the Netherlands)

Don't forget - Mark the post which answered your question with "Answer", then that user will get some kudos, and your post will be marked as "Answered" for future readers!

(VB code examples preferred)
0
KelseyThornton
5/12/2008 1:12:39 PM

but how do I do the click event?

0
supergirl_gem
5/12/2008 3:13:01 PM

jeff_way:

 First, you can do it this way.

Dim imgButton As ImageButton = cType(Master.FindControl("imgButton"), ImageButton)

  

Or..you can expose the control as a public property in the page load event of the Master Page.

 

Public ReadOnly Property lnkbFromMasterPage() As ImageButton
Get
Return imgButton
End Get
End Property

  Then, on your content page, you can access the control by doing...

Master.imgButton
  
This doesn't help me, firstly it doesn't seem to work and secondly you haven't answered my question of how to do the click event.
0
supergirl_gem
5/13/2008 7:55:39 AM

supergirl_gem:

but how do I do the click event?

1. You can try to change the AutoEventWireup to "true"

  • AutoEventWireup="true"

Junior.master source:

<%@ Master Language="C#" MasterPageFile="~/Senior.master" AutoEventWireup="false" CodeFile="Junior.master.cs" Inherits="Junior" %>

2. You can try to Change the Page_Load to Page_PreInit if you are doing dynamic masterpagefile selection

 

For reference:

http://odetocode.com/Blogs/scott/archive/2006/02/16/2914.aspx


. . : : LarnVok : : . .
0
canalso
5/13/2008 9:39:58 AM

Hi,

You can try to refer the below code, which works fine on my local:
1. Expose the ImageButton as a property in the master page's codebehind:
  1. Public Property MPHomeButton() As ImageButton
  2.     Get
  3.         Return Me.HomeButton
  4.     End Get
  5.     Set
  6.         Me.HomeButton = value
  7.     End Set
  8. End Property

2, Add the @MasterType directive for the content page:
<%@ Page Language="C#" MasterPageFile="~/TestMasterPage/case/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="TestMasterPage_case_Default2" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/TestMasterPage/case/MasterPage.master" %>

3. Define a delegate event for the imagebutton in the content page's codebehind:

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  2.     Dim imgBtn As ImageButton = Me.Master.MPHomeButton
  3.     AddHandler imgBtn.Click, AddressOf Button1_Click
  4. End Sub
  5. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  6.     Response.Write("Hello World!")
  7. End Sub

I am not VB guy, the above code is converted by this tool: http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx, so wrong you need to have a debug.

Hope it helps.


Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Amanda
5/14/2008 5:51:24 AM
Reply:

Similar Artilces:

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Why in the world am I getting this error? The solutions I have seen either don't apply or don't work.  Here is my master page code:<%@ Master Language="C#" AutoEventWireup="false" CodeFile="TechLMSMasters/MainSide.master.cs" Inherits="MainSide" %> <%@ Register TagPrefix="usercontrol" TagName="Search" Src="~/library/uc/Search.ascx" %> <%@ Register TagPrefix="usercontrol" TagName="UserName" Src="~/library/uc/UserNameDisplay.ascx" %><!DOCTYPE html PUBLI...

ERROR Content controls have to be top-level controls in a content page or a nested master page that references a master page.
This all used to work until about 5 minutes ago. Did I change something? For some reason I am now getting this error: Content controls have to be top-level controls in a content page or a nested master page that references a master page. What I have:  I have a masterpage inside of a directory. That directory is a first level directory with its own web.config file....The whole thing is protected by the web.config file above it (in the main public directory). That main public directory has a masterpage as well. I am not currently linking the two masterpages together becuase...

Error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.
when I make a directory on my root, and in that directory I put a masterpage and a web form ... I get always an error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.   Does somebody know what the problem is ? Is it possible you can't set a masterpage in a directory ... ?Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) i've founded ... I had to nest my masterpage ...Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) Hi Joeri Pansaerts, I am receiving the same error as the one posted her...

Problem to use TreeView Control in a 'content page' This Error Occur "Only Content controls are allowed directly in a content page that contains Content controls."
hi I am trying to use the TreeView Control inside the content page (Page that has a master page is a content page). On compiling the project I got the following ASP.Net runtime error: "Only Content controls are allowed directly in a content page that contains Content controls."B@b@R J Issue resolved. I was putting script tag in the content page that is not allowed. when remove the script from content page problem is resolved.B@b@R J...

accessing controls in master page and adding event to content page
Hi  I've currently got a control on my master page and i'm trying to write an even on the content page that fires when the button is clicked, I don't think it is working though. Can someone help? The code is below:  masterpage <asp:ImageButton id="imgButton" runat="server"/> code behind Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load, Me.Load Public ReadOnly Property lnkbFromMasterPage() As ImageButtonGetReturn imgButtonEnd GetEnd Property  content page code behind: Public Sub Page_Load(ByVa...

Using controls from pages, master pages, and user controls
How would I structure my site in this scenario: I'd like to use a dropdownlist all over my site, sometimes in a regular page, sometimes in a master page, and sometimes in a user control.  I wish to bind its events programmatically, so normally I would just put that in the codebehind.  However, I notice a lot of code duplication because I have to declare the control in everywhere, in my base page (which all my pages inherit from), in my base master page (which all my master pages inherit from) and in my base user control (which all my user controls inherit from).How should I be...

User Control in Master page raises "clicked" event...how to subscribe to this event in "nested" Master page??
I have a custom user control with several LinkButtons that serve as a common menu for all my pages.  Within this user control, I created a delegate and a custom event...and raise a "clicked" event whenever one of the LinkButtons is clicked. I added this user control to a Master page, then subscribed to that "clicked" event in the Page_Load.  The subscribed event handler within this Master page works fine. My problem is...I created a "nested" Master page from the Master page above, and want to subscribe to the original user control...

Using Master Page controls in content pages..
Dear All I am new to using Master pages but i find them very usefull. What I have is on the master page is a menu that consists of series of link buttons that link to different content pages and a series of panels that contains an image of a bar that correspondes to a button, and I wont to set the visibility of a panel to true or false depending on the click of a link button. The problem is the panels on the master page seem to be protected, and when i go to reference the control in the content page i cant even though the content page now about the control. here is the code i am using...

master page treeview control treenodechanged event called from my content page but in one case event does not called
Hi All, I have a Treeview control at my master page and from my content page I show user interface based on TreeNodeChanged, Treenodechanged event from my content page but in one case this event does not executing.  I checked using trace and debug but this event does not fired from my content page in one case only, all other case it work perfectly. // here I am triggering node changeEvent but in one case it does not even called................protected void Page_Init(object sender, EventArgs e) {Trace.Write("Content Page", "Page_Init"); myMaster = (Ma...

Unable to validate textboxex and other controls using java script while iam using master pages and content page
Hi all,   iam unable to validate textboxes and other controls using java script  while iam using Master page i have added controls to content page and have to Perform validation using java script . can any body guide me in this issue while dealing with master Pages . where i have to put java script validation code . actullay nothing happen when iam using Master pages .     best regards rameez  life is name of learning!Mark as an answer if it helps Master pages mangle the control IDs. Use this to find the correct ID from JavaScript: var textBo...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

using findcontrol in content page to find master page control
 hi all,            I am trying to find a panel on a master page from its content page. But the panel is always coming null. This is my code:        ContentPlaceHolder cnt = new ContentPlaceHolder();        cnt = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");        Panel pnl = new Panel();        pnl = (Panel)cnt.FindControl("pnlLogin");      &nb...

Composite control EVENT NOT FIRING when using with page having a master page
 Hi All am creating a custom composite control to upload file having and FileUpload and Button inside my custome control. My control is working fine with stand alone page (page which don't have master page) but when i use with page having master page the Button click event used inside my custom control dont get fired. So plz anybody help me. My reduced code is Normal 0 using System; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;...

Web resources about - using master page controls in content page and adding click event - asp.net.navigation-controls

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Firefighters battle out of control bushfire in Bullsbrook
Around 70 firefighters are battling an out of control bush-fire in Bullsbrook.

Firefighters battle out of control bushfire in Bullsbrook
Around 70 firefighters are battling an out of control bush-fire in Bullsbrook.

US Democratic candidates clash over gun control at debate
Hillary Clinton criticised over foreign policy and gun control but again confirms her position as frontrunner for the Democratic nomination. ...

Victorian fires out of control
Residents are being urged to evacuate as an out of control bushfire near the Indigo Valley in northern Victoria threatens homes. Courtesy ABC ...

Victoria bushfires: Out-of-control blazes in Victoria as heatwave continues across southern Australia ...
Victoria's Country Fire Authority says houses are likely to be impacted by a 3,000-hectare bushfire west of Melbourne.

Stringify your life by taking control and automating tasks
... Adobe Post or If you can think it, you can create it in Scribblenauts Unlimited . Stringify IF by IFTTT Stringify your life by taking control ...

How to engage Tesla's Launch Control mode
Filed under: Green , Videos , Tesla , Electric Short video shows Model S keeping it straight on a rain-slicked road.

Tencent Holdings Takes Full Control of 'League of Legends' Developer Riot Games
Breitbart News Tencent Holdings Takes Full Control of 'League of Legends' Developer Riot Games Breitbart News Riot Games, the creators of ...

China's Xi on cyberspace control
Chinese President Xi Jinping defended his country's right to censor Internet content, signaling confidence in the strategy.

Resources last updated: 12/20/2015 7:45:59 PM