using form.findcontrol in a masterpage

Last post 02-12-2008 12:39 AM by Amanda Wang - MSFT. 7 replies.

Sort Posts:

  • using form.findcontrol in a masterpage

    02-08-2008, 2:59 PM
    • Participant
      1,717 point Participant
    • Fonzie
    • Member since 06-10-2005, 2:10 PM
    • Posts 865

    I get an error when referencing the following in a page that uses a masterpage

    "form1.findcontrol:"

    What would I use instead of "form1"

     

     

  • Re: using form.findcontrol in a masterpage

    02-08-2008, 3:08 PM
    • Member
      46 point Member
    • Twood
    • Member since 05-22-2007, 5:54 PM
    • Posts 97

    try me.findcontrol() 

     

    me.form1.findcontrol should also work. 

    .net developer - Charlotte, NC
  • Re: using form.findcontrol in a masterpage

    02-08-2008, 6:03 PM
    • Member
      136 point Member
    • jll32
    • Member since 01-31-2008, 6:06 PM
    • Posts 27

    I don't think Me.FindControl("mycontrol") will work to find a control on the Master Page.

    However, Master.FindControl("mycontrol") will work, as will Me.Form.FindControl("mycontrol").

    For example, if you have a textbox "textbox1" on your Master Page, you can call it from the code behind of your Page like this:

    Dim mytextbox As TextBox = Me.FindControl("textbox1")
    mytextbox.Text =
    "hello world!"

    Thanks

  • Re: using form.findcontrol in a masterpage

    02-11-2008, 3:33 AM

    Hi,

    Do you want to access the master page's controls in the content page that refer the masere page, right?

    You should use the page.master.findcontrol.

    And you need  have to be careful with FindControl, you have to be careful with client side script functions like getElementById. You should use the correct client side ID of the control.

    The FindControl documentation on MSDN is FindControl searches the current naming container for the specified server control. A naming container is any control that carries the INamingContainer interface. Both the MasterPage and Content controls are naming containers. The key to using FindControl is to invoke the method on the correct container, because FindControl doesn’t recursively traverse the entire hierarchy of controls. FindControl only searches inside the current naming container. Using the FindControl method on the Page reference means we won’t be searching inside of MasterPage control.   

    The more you can refer the section of the FindControl, JavaScript, and Naming Containers of this article: http://www.odetocode.com/Articles/450.aspx

     

    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.
  • Re: using form.findcontrol in a masterpage

    02-11-2008, 3:59 AM
    Answer
    • Participant
      1,360 point Participant
    • shayas
    • Member since 02-06-2008, 12:42 PM
    • Sharjah,UAE
    • Posts 273

    hi

    try to make the control as public property for

    example

    consider label lblExample

    in master page

    public property LabelExample as Label

    Get

    Return lblExample

    End Get

    Set (Byval Value as Label)

    lblExample=value

    end set

    end property

     to access it from master and add  this <%@ MasterType VirtualPath="~/urmasterpage.master" %> in ur content page

    and access it in code behind as

    Master.LabelExample

    Yasser
    Spica.
    www.spica.ae
    www.gccpsg.com/gccnew
    www.snasco.com
    www.bncnetwork.net
    Please remember to mark as answers if this helps
  • Re: using form.findcontrol in a masterpage

    02-11-2008, 10:08 AM
    • Participant
      1,717 point Participant
    • Fonzie
    • Member since 06-10-2005, 2:10 PM
    • Posts 865

    Well I'm not sure if master page is in the container or vice versa. Here is the declaration I'm using in the .aspx file.

    <%@ Page Language="VB" masterpagefile="~/Lmasterpage.master" AutoEventWireup="false" CodeFile="benchstockP.aspx.vb" Inherits="benchstockP" EnableSessionState="True" %>

    <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">

  • Re: using form.findcontrol in a masterpage

    02-12-2008, 12:34 AM
    Answer
    • Participant
      1,360 point Participant
    • shayas
    • Member since 02-06-2008, 12:42 PM
    • Sharjah,UAE
    • Posts 273
    Fonzie:

    <%@ Page Language="VB" masterpagefile="~/Lmasterpage.master" AutoEventWireup="false" CodeFile="benchstockP.aspx.vb" Inherits="benchstockP" EnableSessionState="True" %>

    after this add

     <%@ MasterType VirtualPath="~/urmasterpage.master" %>

    and then

    access the label

    in contnt page code behind

    as

    Master.LabelExample(ur label prperty)

    Yasser
    Spica.
    www.spica.ae
    www.gccpsg.com/gccnew
    www.snasco.com
    www.bncnetwork.net
    Please remember to mark as answers if this helps
  • Re: using form.findcontrol in a masterpage

    02-12-2008, 12:39 AM
    Answer

    Hi,

    The master page is a naming container, if there is a textbox on the master page and its id is TextBox1 , but you will find its clientid is ctl00$TextBox1 at the render time, the "ctl00" is the master page's defautl id.

    If the textbox1 is on the content page, you will find the textbox's clientid is ctl00$contentPlaceHolder$TextBox1, the "ctl00" is the master page's defautl id, the "contentPlaceHolder" is the contentplaceholder's id that is on the master page.

    So if you want to use the findcontrol or the javascript's getElementById, you should be careful, you should use the control's clientID, you can get the control's clientid by view the page source at the render time.

    A better approach is to establish a formal relationship between the master page and content page, and take advantage of strong typing. Instead of the content page poking around inside the master page, you can export the controls as the public properties in the master page codebehind,

    You can try to add the following code to our master page.

    Public Property FooterText() As String
      Get
        Return FooterLabel.Text
      End Get
      Set(ByVal value As String)
        FooterLabel.Text = value
      End Set
    End Property

    Then, try to use the property is to place a @ MasterType directive in our content page. When the ASP.NET compiler sees the @ MasterType directive, it creates a strongly typed Master property in our Page derived class.

    <%@ Page Language="VB" MasterPageFile="~/Master1.master"
             AutoEventWireup="true"  %>
    <%@ MasterType VirtualPath="~/Master1.master" %>

    <script runat="server">
     
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Master.FooterText = "Custom footer text"
       
      End Sub
     
    </script>

    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.
Page 1 of 1 (8 items)