Cómo hacer referencia a los controles de una MasterPage en ASP.NET

En este artículo veremos cómo hacer referencia a las propiedades de los controles de una página maestra desde nuestra página web.


 

Utilizaremos el método FindControl, para buscar y hacer referencia al control de la Página Maestra.

En el ejemplo siguiente se muestra el uso del método FindControl para obtener una referencia a dos controles de la página maestra, un control TextBox y un control Label. Dado que el control TextBox está dentro de un control ContentPlaceHolder, debe obtener primero una referencia a ContentPlaceHolder y, a continuación, utilizar su método FindControl para buscar el control TextBox.

VB

Dim mpContentPlaceHolder As ContentPlaceHolder
    Dim mpTextBox As TextBox
    mpContentPlaceHolder = _
        CType(Master.FindControl("ContentPlaceHolder1"), _
        ContentPlaceHolder)
    If Not mpContentPlaceHolder Is Nothing Then
        mpTextBox = CType(mpContentPlaceHolder. _
            FindControl("TextBox1"), TextBox)
        If Not mpTextBox Is Nothing Then
            mpTextBox.Text = "TextBox found!"
        End If
    End If

    ' Gets a reference to a Label control not in a 
    ' ContentPlaceHolder
    Dim mpLabel As Label
    mpLabel = CType(Master.FindControl("masterPageLabel"), Label)
    If Not mpLabel Is Nothing Then
        Label1.Text = "Master page label = " + mpLabel.Text
    End If

C#

// Gets a reference to a TextBox control inside 
    // a ContentPlaceHolder
    ContentPlaceHolder mpContentPlaceHolder;
    TextBox mpTextBox;
    mpContentPlaceHolder = 
      (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
    if(mpContentPlaceHolder != null)
    {
        mpTextBox = 
            (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
        if(mpTextBox != null)
        {
            mpTextBox.Text = "TextBox found!";
        }
    }
    
    // Gets a reference to a Label control that not in 
    // a ContentPlaceHolder
    Label mpLabel = (Label) Master.FindControl("masterPageLabel");
    if(mpLabel != null)
    {
        Label1.Text = "Master page label = " + mpLabel.Text;
    }

 

Artículos relacionados
12 Comentarios
  • joel peña Comentar
    Tuesday, May 26, 2015

    Referencia a objeto no establecida como instancia de un objeto.

  • Peralta Comentar
    Friday, March 9, 2018

    Cho tôi h?i ? TPHCM ch? nào di?u tr? sùi mào gà Tuy?t nhiên nh?t ?? tôi ? t?n Bình Thu?n nên cháu k n?m rõ ch? nào th?p d? di tham khám . AloBacsi có kh? nang ch? giúp cháu du?c không ?? tôi xin c?m on AloBacsi m?t s? . (Thanh Nhan Nguyen) AloBacsi tr? l?i: Chào b?n, tru?c dây, sùi mào gà du?c xem là m?t b?nh lí lành tính. nhung mà ngày nay , m?t s? nhà khoa h?c cho r?ng trong các gi? d?, b?nh lý th?y xu hu?ng vuon lên là ác tính, d?n d?n ung thu c? t? cung ho?c c?a quý . d? tu?i m?c b?nh nhi?u nh?t là 20-25. b?nh lý này thu?ng vì 2 nguyên do d?n d?n ra: + do giao hoan mang ngu?i dã và dang mang b?nh . + do v? sinh không làm s?ch s?. + thông qua m?c chung d? v?i ngu?i dang m?c b?nh + vì ngu?n g?c nu?c b?n áp d?ng 0 ?n. lây b?nh lý này b?n có nguy co làm ki?m tra và tr? t?i BV Da Li?u ho?c BV T? Du. Tùy th?i gian n?ng nh? c?a b?nh nam khoa s? hình thành li?u trình ch?a tr? có lí nhu: d?t di?n, ch?m thu?c. b?t bu?c kiên trì ch?a tr? b?nh tình s? phòng tránh kh?i, gi? d? d? lâu s? khó h?n ch? qua b?nh lý s? truy?n nhi?m ti?t m?t s? vùng khác trên s?c kh?e. B?n có nguy co g?i nhi?u b?nh xá sau d? du?c ki?m tra tham khám và d? phòng k?p th?i nhé: BV Da Li?u TPHCM 2 Nguy?n Thông, Phu?ng 6, Qu?n 3, TPHCM ÐT: 08 3930 5995 du?ng dây nóng: 0908 051 200 D?ch v? d?t l?ch h?n khám b?nh lý 0909 100 819 Ð? k truy?n cò m?i d? d? lúc t?i khám t?i BV Da Li?u b?n có kh? nang B? sung thêm bài vi?t sau d? ch? d?ng hon lúc di l?i . >> phác d? xét nghi?m b?nh nguy nguy hi?m t?i BV Da Li?u TPHCM BV T? Du TPHCM 106 C?ng Qu?nh, Phu?ng Ph?m Ngu Lão, Qu?n m?t, TPHCM ÐT: 08 5404 2829 Mong r?ng m?t s? tin t?c ? trên s? giúp ích cho b?n. Thân ái,

  • Wortham Comentar
    Monday, March 12, 2018

    Excellent post. I was checking constantly this blog and I am impressed! Very useful information particularly the last part :) I care for such information a lot. I was looking for this particular information for a long time. Thank you and good luck.

  • Musgrove Comentar
    Saturday, March 17, 2018

    It's not my first time to pay a quick visit this site, i am browsing this website dailly and get pleasant information from here daily.

  • South Comentar
    Sunday, March 18, 2018

    casino games slots casino games slots online casino slot online casino online

  • Zielinski Comentar
    Friday, March 23, 2018

    Good day! This post couldn't be written any better! Reading this post reminds me of my good old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Many thanks for sharing!

  • Mckenna Comentar
    Wednesday, March 28, 2018

    Nn j’ai attendu 2 min et sa marche pas

  • Murph Comentar
    Thursday, March 29, 2018

    Are there any clash royale dns articles

  • Marchant Comentar
    Wednesday, April 4, 2018

    Hola amigo, a mi no me aparece? y colocó en el buscar de tutuapp alfa tournaments y no aparece también, como soluciono? me puedes ayudar por favor

  • Bobadilla Comentar
    Wednesday, April 4, 2018

    Hi, every time i used to check blog posts here in the early hours in the daylight, because i like to find out more and more.

  • Pantano Comentar
    Friday, May 25, 2018

    The other day, while I was at work, my cousin stole my iphone and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is totally off topic but I had to share it with someone!

  • Chatham Comentar
    Friday, May 25, 2018

    It's a pity you don't have a donate button! I'd certainly donate to this superb blog! I suppose for now i'll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this website with my Facebook group. Chat soon!

Enviame un comentario