Click here to Skip to main content
15,885,000 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My ContentPlaceHolder:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Dang.aspx.cs" Inherits="Dang" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<asp:Content ID="Content2" runat="server" contentplaceholderid="Content1">
    <asp:TextBox ID="txt" runat="server">

You can see Content1 and Content2.How to Fincontrol in ContentPlaceHolder?
I tried:
C#
TextBox t =(TextBox) Master.FindControl("Content1").FindControl("txt");
TextBox txtuser = (TextBox)Page.Master.FindControl("txtUser");
t.Text = txtuser.Text;

Masterpage:
C#
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUser.Text = "John";
    }
}

But error occurs.
Posted
Updated 16-May-12 21:49pm
v2

1 solution

if you want to try find any control of content page in master page do this

C#
//Content1 is your ContentPlaceHolder id in master page
TextBox t =(TextBox)Content1.FindControl("txt");
 
Share this answer
 
v2
Comments
giatuan2011 17-May-12 4:01am    
I tried your code and:
ContentPlaceHolder con = (ContentPlaceHolder)Page.Master.FindControl("Content1");
TextBox t = (TextBox)con.FindControl("txt");
TextBox txtuser = (TextBox)Page.Master.FindControl("txtUser");
t.Text is empty
Dain Ucak 17-May-12 5:15am    
do you try this in your master page and whats your ContentPlaceHolder Id?
giatuan2011 17-May-12 6:22am    
My ContentPlaceHolder Id is Content1 in Master.Asp.net 2005 and asp.net 2008 are different?What is Content2?
make Dang.aspx-> select master-> in design, I can not add anything to the ContentPlaceHolder.
I click CustomContent .. and Content 2 appears.
This is a problem?
Dain Ucak 17-May-12 6:40am    
when you create a master page it create two contentplaceholders. one of them for head tags and other one for bodys. if you want to get any controls of content page like dang.aspx in your master page. TextBox txt = (TextBox)Content1.FindControl("txt"). this is enough for get any controls from content pages. if you didnt solve pls post your master page and content pages code. i will analyze it for you
giatuan2011 17-May-12 8:02am    
TextBox t = (TextBox)con.FindControl("txt");
TextBox txtuser = (TextBox)Page.Master.FindControl("txtUser");
I tried:
t.Text = "Hello";
t.Text = txtuser.Text.ToString();
It appears "Hello" in textbox of the page Dang.aspx.
txtUser not change.
I not found Content1.It only has ContentPlaceHolder,Content,Page and Master.
How can I attach files?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900