CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 2/7/2006 2:31:28 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 9 Views: 168 Favorited: 0 Favorite
10 Items, 1 Pages 1 |< << Go >> >|
mickyjtwin
Asp.Net User
Find Control from Inherited Master Page2/7/2006 2:31:28 PM

0

This is my problem(it's killing me!!)

I have a master page for my homepage(site.master):
<asp:ContentPlaceHolder ID="Content1" runat="server">
   <asp:ContentPlaceHolder ID="mainContentLeft" runat="server"></asp:ContentPlaceHolder>
   <asp:ContentPlaceHolder ID="mainContentRight" runat="server"></asp:ContentPlaceHolder>
</asp:ContentPlaceHolder>

I then have an inherited master page for my content pages(content.master):
<%@ Master MasterPageFile="~/site.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content1" runat="server">
   <asp:ContentPlaceHolder ID="mainContent" runat="server"></asp:ContentPlaceHolder>
</asp:Content>

I have a content page called events.aspx, which contains a hidden field and a button which posts to signup.aspx.
I want to get the value of the hidden field from the events page in the codebehind of signup. This is only causing me issues because of the inherited master page. This is my code so far in signup.aspx.vb:
(FYI, i tested if I get into the If statement, and i do, just not getting the value. Always get the same error: Object reference not set to an instance of an object.
If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
Dim EventID As HiddenField
   'Have tried alot of different variations of PreviousPage.Master.Master etc.
   EventID =
CType(PreviousPage.FindControl("hdnEvent"), HiddenField)
   lblError.Text = EventID.Value.ToString()
End If

ClayCo
Asp.Net User
Re: Find Control from Inherited Master Page2/7/2006 5:47:42 PM

0

Hey Micky,

Calling PreviousPage after posting from a content page will return a value that's typed to the top-level master page.  In your case, to get the content of the hidden field, you should replace your call to PreviousPage.FindControl() with:

CType(PreviousPage.Form.FindControl("ContentPlaceHolder1").FindControl("ContentPlaceHolder1").FindControl("HiddenField1"), HiddenField)

Of course, you'll probably need to replace the control names used here with those from your actual application.  The above should return the expected control instance.

HTH,
Clay

 

mickyjtwin
Asp.Net User
Re: Find Control from Inherited Master Page2/7/2006 6:15:19 PM

0

According to my initial code, and your response Clay, I start at the root and work down.
So, my root content placeholder is Content1, and the contents page holder is mainContent.

Dim EventID As HiddenField
EventID = CType(PreviousPage.Form.FindControl("Content1").FindControl("mainContent").FindControl("hdnEvent"), HiddenField)

Still gives me an error. I am not sure if I'm interpreting your comment, and my code correctly.|

My display string after the CType is
lblMessage.Text = EventID.Value.ToString()

Any ideas as to why this isn't working?
M

ClayCo
Asp.Net User
Re: Find Control from Inherited Master Page2/7/2006 8:36:50 PM

0

In my own investigation, I used the default Visual Studio template for the top-level master, which contains its own <form> tags, and my response assumed you had the same structure.  However, a careful re-reading of your original post suggests otherwise, and judging from your source code it appears that the <form> is in your content page.  Sorry about not catching that earlier.

This is relevant because the control on which you need to call FindControl() needs to be no farther above the control in the page's control tree than the lowest-level naming container that contains the control -- naming containers being the controls whose IDs are strung together to form the unique ID for the control that you see when you view source.   If the Form is in the content page's Content control, than the form is the naming container, otherwise the Content control is. 

That may be a litle hard to follow, but the impact on you is easy.  Instrad of calling the above, you would then be able to simply call:

EventID = CType(PreviousPage.Form.FindControl("hdnEvent"), HiddenField)

And that should work for you (again, assuming the <form> is where I think it is.)

Thanks,
Clay

mickyjtwin
Asp.Net User
Re: Find Control from Inherited Master Page2/7/2006 9:52:59 PM

0

Like I said, this problem is going to kill me!! Thanks for all the help so far, I think I'll give you some further code on my project.

site.master (i took out my div's for ease of reading. The homepage is default.aspx)
<form id="form1" runat
="server">
<asp:ContentPlaceHolder ID="Content1" runat="server">

   <asp:ContentPlaceHolder ID="mainContentLeft" runat="server"></asp:ContentPlaceHolder>
   <asp:ContentPlaceHolder ID="mainContentRight" runat="server"></asp:ContentPlaceHolder>
</asp:ContentPlaceHolder>
</form>

content.master (all content pages use this as their master file)
<%@ Master MasterPageFile="~/site.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content1" runat="server">
   <asp:ContentPlaceHolder ID="mainContent" runat="server"></asp:ContentPlaceHolder>
</asp:Content>

My page events.aspx has a hidden field and a link button to signup.aspx
<asp:HiddenField ID="hdnEvent" runat="server" Value="2" />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="signup.aspx">Signup</asp:LinkButton>

The codebehind for signup.aspx.vb is as follows:
Partial Class signup
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
      Dim EventID As HiddenField
      EventID =
CType(PreviousPage.Form.FindControl("hdnEvent"), HiddenField)
      lblMessage.Text = EventID.Value.ToString()

   End If
End Sub

I hope this can maybe shed some further light onto my dilema. You have been great so far Clay. Thanks for your consistent attention to my problem.
I think after this(and understanding) I should write a book on finding controls in inherited masterpages!!

M

mosessaur
Asp.Net User
Re: Find Control from Inherited Master Page2/8/2006 10:49:30 AM

0

I have a suggestion, if events.aspx page is the only page which submits to signup.aspx then add this directive to your signup.aspx:
<%@ PreviousPageType VirtualPath="~/events.aspx"%>

Also expose your hidden field in your events.aspx by a public readonly property

then use is like this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
      Dim EventID As HiddenField
      EventID =
PreviousPage.EventIDHiddenField
      lblMessage.Text = EventID.Value

   End If
End Sub

Try it and inform us with the results, also this is a reference for the mentioned directive above
http://msdn2.microsoft.com/ms228169.aspx

Good luck


Muhammad M. Mosa Soliman
Software Engineer
Blog | Live Space
mickyjtwin
Asp.Net User
Re: Find Control from Inherited Master Page2/8/2006 2:29:01 PM

0

Some more helpful(maybe) info.

I actually have a textbox within the site.master file. It is used for searching the site. This posts to search.aspx(which uses content.master). In my codebehind for search.aspx, I have this...

Dim SearchTerm As TextBox
lblSearch.Text = PreviousPage.Master.FindControl(
"txtSearchTerm").ToString()

For the first post, i.e. from the homepage(site.master) it works fine. The next post, from search.aspx, gives me the error again. I know this is related to my issue of having content.master inherited from site.master. I have tried numerous variations to the above to my original problem, some educated, some not, and I can't figure it out.
HELP!!!!

Mick

mickyjtwin
Asp.Net User
Re: Find Control from Inherited Master Page2/8/2006 4:24:07 PM

0

Thanks for the suggestion on exposing the hidden field. As it turns out, signup.aspx always comes from events.aspx, so it works perfectly. However, I would still like to know how to reference the object without doing this, once, just so I can understand it, and because I believe I will need to know that for other requirements on my website.

Thanks again mohammed,

Mick

mickyjtwin
Asp.Net User
Re: Find Control from Inherited Master Page2/8/2006 4:57:56 PM

0

In my joy of finding a solution, I have since realised this might not work. With the hiddenfield placed on the page, it works no problem, however my hiddenfield is within a FormView, as it is generated dynamically. Once I place the hiddenfield inside the FormView, my public readonly return hdnEvent returns an error or hdnEvent is not declared. Within the ItemTemplate my hdnEvent is as such:

<asp:HiddenField ID="hdnEvent" runat="server" Value='<%# Eval("EventID") %>' />

Is it because this hiddenfield is dynamic that the public readonly won't find it? Now I guess I must go back to my original scenario, which still isn't resolved!! LOL
Gotta love it,

Mick

mickyjtwin
Asp.Net User
Re: Find Control from Inherited Master Page2/10/2006 3:15:19 PM

0

So I might be the only one who has this problem, but nevertheless, I figured it out finally.
Once I put everything on paper, it because pretty clear, along with the advice of previous posts.
What I didn't realise, was that I had to include my formview container as a control. That's it!!!
So my line ended up:

EventID = CType(PreviousPage.Form.FindControl("Content1").FindControl("mainContent").FindControl("viewEvent").FindControl("HiddenField1"), HiddenField)

Content1 is in my Site.Master Page, mainContent is in my Content.Master Page, which inherits from site.master. The viewEvent(FormView) is in mainContent. Follow the chain, and lo and behold, the value popped up.

Thanks to all who helped and got me on the right track!!!!

Mick

10 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
ASP.NET 2.0 Website Programming: Problem-design-solution Authors: Marco Bellinaso, Pages: 576, Published: 2006
Microsoft Office SharePoint Server 2007: The Complete Reference Authors: David Matthew Sterling, David Sterling, Pages: 788, Published: 2007
Professional ASP.NET 2.0 Authors: Bill Evjen, Scott Hanselman, Farhan Muhammad, Srinivasa Sivakumar, Devin Rader, Pages: 1253, Published: 2005
Essential ASP.Net 2.0 Authors: Fritz Onion, Keith Brown, Pages: 345, Published: 2006
Pro ASP.NET 2.0 Website Programming Authors: Damon Armstrong, Pages: 641, Published: 2005
Pro ASP.NET 2.0 in C# 2005 Authors: Matthew MacDonald, Mario Szpuszta, Pages: 1255, Published: 2005
Pro ASP.NET 2.0 in C# 2005: Create Next-generation Web Applications with the Latest Version of Microsoft's Revolutionary Technology Authors: Matthew MacDonald, Mario Szpuszta, Pages: 1426, Published: 2006
Pro ASP.NET 2.0 E-commerce in C# 2005 Authors: Paul Sarknas, Pages: 617, Published: 2006
Professional ASP.NET 3.5: In C# and VB Authors: Bill Evjen, Scott Hanselman, Devin Rader, Pages: 1673, Published: 2008
Professional ASP.NET 2.0 Databases Authors: Thiru Thangarathinam, Pages: 504, Published: 2007

Web:
MasterPage Methods (System.Web.UI) MasterPage Methods. Updated: November 2007. The MasterPage type exposes the ... If it does not, it creates child controls. (Inherited from Control.) ...
MasterPage Properties (System.Web.UI) (Inherited from Control.) Master, Gets the parent master page of the current ... a server control is rendered as UI on the page. (Inherited from Control.) ...
Inherited Controls and Master Pages - ASP.NET Forums Inherited Controls and Master Pages. Last post 05-14-2008 10:36 PM by Amanda ... Just as we have to be careful with FindControl, we have to be careful with ...
CodeProject: Inside Master Pages. Free source code and programming ... Aug 18, 2005 ... findcontrol inside tab container in the nested masterpage, member .... call the ClientID property of any control inherited from System.Web. ...
Master Pages in ASP .Net 2.0 Apr 17, 2005 ... See the first line, you will find it amazing but yes you can do like this. You can inherit or nest one master page inside the other. ...
Michael Sync » Using Yahoo.UI.Calendar in PageTemplate and Master ... Master Page. This class is inherited from System.Web.UI.Page. ..... Find ( control+F) “showCalander1″ function in the entire solution file. ...
Master Page Inheritance and User Controls - Rick Strahl's Web Log NET 2.0 - this time in relation to Master Pages. Looks like on inherited ... master involves getting references to anything using FindControl() which is ...
Silverlight Control does not gets displayed when host page ... But when i put silverlight control on my existing page of web application which is inherited from my custom base page class, the control ...
Master page - ASP Free this code is in the masterpage and we inherited the masterpage in my page. i dont know why they are utilising server control instead of ...
Beginning ASP.NET 2.0 Page so Master Page objects are inherited from System.Web.UI.MasterPage. .... load the user control dynamically other than finding control and loading them. ...

Videos:
Charlie Rose - Henry Louis Gates, Jr. / Spencer Wells / Major Gen. Douglas Lute Segment 1: Henry Louis Gates, Jr. of Harvard University talks about his latest project for PBS, African American Lives. Segment 2: Spencer Wells i...
Charlie Rose - Judy Woodruff & Mike Allen / Wole Soyinka / Jeremy Allaire Segment 1: Guest host Judy Woodruff talks politics with Mike Allen of Time magazine. Segment 2: Nobel laureate Wole Soyinka talks with guest host ...
Cartoons are evil part 2 Cartoons are evil DVLH http://www.lukehadley.com Scoobert "Scooby"-Doo is a fictional dog and the eponymous character of the popular television serie...
Cartoons are evil part 3 Cartoons are evil DVLH http://www.lukehadley.com Scoobert "Scooby"-Doo is a fictional dog and the eponymous character of the popular television serie...
Zeitgeist 2 : Addendum - Subtitles English / French Zeitgeist: Addendum, by Peter Joseph 2008 English Subtitles ! French See ya later on thezeitgeistmovement.com You can Download Srt file here : EN:...












are there any set up files in asp.net ....

master page compiling

trouble applying skinid to an <asp:tablerow> tag

sitemapnode title

applying skins and themes

master page???

how do i load an iframe into a contentholderplace from a masterpage?

how to apply templates in asp.net

skmmenu issues...

treeview menu

external master

how do i create an asp.net website template for new sites that use my masterpage?

working with asp:menu 2005. can u help me?

problem with menu webcontrol

on mouse roll over..

web.sitemap problem

page in sitemap, but not on menu

get selected value for sqldatasource select parameter located in content page from listbox located in master page

styles for menu control

sitemappath with image as pathseparator?

change menu in master page

contentplaceholder height

changing the menu user control programmatically

date and time

navigation objects [not impressed]

binding grid through master page

master page in different directory

master page issues with sqldatasource

i have problem with site map path and master page ?

treeview root node text click

 
Search This Site:

 
  Privacy | Contact Us
All Times Are GMT