locked
Problem Findcontrol and Masterpage RRS feed

  • Question

  • User-1663731923 posted
    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
    Sunday, July 2, 2006 9:05 AM

All replies

  • User445179017 posted
    Hi,

    Have a look at this Master Pages and FindControl()

    Thanks
    Sunday, July 2, 2006 12:59 PM
  • User-587754273 posted
    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.


    Sunday, July 2, 2006 9:54 PM
  • User-1663731923 posted

    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

    Sunday, July 9, 2006 4:12 PM
  • User335614965 posted

    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

    Friday, December 7, 2007 10:39 AM