Problem Findcontrol and Masterpage

Last post 12-07-2007 10:39 AM by DB3. 4 replies.

Sort Posts:

  • Problem Findcontrol and Masterpage

    07-02-2006, 9:05 AM
    • Loading...
    • Eriksk
    • Joined on 06-14-2005, 8:45 AM
    • The Netherlands - Achterhoek
    • Posts 119
    Hi all,

    I've got a problem with the findcontrol function. I use this function in a usercontrol to retrieve a value from an hidden field in the page (Dim iTypeOfNews As Integer = Convert.ToInt16(CType(Page.FindControl("fldNews"), HiddenField).Value)). It all worked fine, untill I put that page in a contenplaceholder in a masterpage. The function can't find the hiddenfield anymore.
     The code:
    <%@ Page Language="VB" MasterPageFile="~/Main.master" Trace="true"  AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" title="Untitled Page" %>
    
    <%@ Register Src="UserControls/News.ascx" TagName="News" TagPrefix="uc1" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" Runat="Server">
        <asp:HiddenField ID="fldNews" runat="server" Value="1" />
        <uc1:News ID="News1" runat="server" />
    </asp:Content>

     Has someone a solution?

    Thanks in advance.
    Regards,
    Erik
    “If something can go wrong, it will”
  • Re: Problem Findcontrol and Masterpage

    07-02-2006, 12:59 PM
    • Loading...
    • e_screw
    • Joined on 10-20-2004, 9:22 AM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,852
    Hi,

    Have a look at this Master Pages and FindControl()

    Thanks
    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
  • Re: Problem Findcontrol and Masterpage

    07-02-2006, 9:54 PM
    • Loading...
    • jasonli
    • Joined on 08-03-2005, 7:23 PM
    • Posts 262
    I think you need to add a statement in the ASPX page which contains the user control.

    Just after the <%page> directive, add this:
    <%@master virtualpath="~/masterpage.master">

    Then you can access the controls that located in master page.


  • Re: Problem Findcontrol and Masterpage

    07-09-2006, 4:12 PM
    • Loading...
    • Eriksk
    • Joined on 06-14-2005, 8:45 AM
    • The Netherlands - Achterhoek
    • Posts 119

    Me

    .Parent.FindControl("fldNews")

    was enough (notice 'parent').

    I always used an recursive function, but that dind't work either... after alot of debugging in that function, I found out the container was wrong and I needed it's parent.

    Thanks guys

    “If something can go wrong, it will”
  • Re: Problem Findcontrol and Masterpage

    12-07-2007, 10:39 AM
    • Loading...
    • DB3
    • Joined on 05-08-2007, 3:46 PM
    • Posts 1

    Here is a code snippet that I came up with to help all of you out there.

     

    Dim _myControl As WebControl = Master.FindControl("ContentPlaceHolder1").FindControl(ControlToEnable)
    _myControl.Visible = True

Page 1 of 1 (5 items)