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:
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.
Eriksk
Member
573 Points
119 Posts
Problem Findcontrol and Masterpage
Jul 02, 2006 01:05 PM|LINK
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
e_screw
All-Star
19528 Points
3893 Posts
Re: Problem Findcontrol and Masterpage
Jul 02, 2006 04:59 PM|LINK
Have a look at this Master Pages and FindControl()
Thanks
Electronic Screw
Website||Blog||Dub@i.net
jasonli
Participant
1191 Points
268 Posts
Re: Problem Findcontrol and Masterpage
Jul 03, 2006 01:54 AM|LINK
Just after the <%page> directive, add this:
<%@master virtualpath="~/masterpage.master">
Then you can access the controls that located in master page.
Eriksk
Member
573 Points
119 Posts
Re: Problem Findcontrol and Masterpage
Jul 09, 2006 08:12 PM|LINK
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
DB3
Member
2 Points
1 Post
Re: Problem Findcontrol and Masterpage
Dec 07, 2007 02:39 PM|LINK
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
master page find control