ContentPlaceHolder中的FindControl问题 [英] Problem FindControl in ContentPlaceHolder

查看:71
本文介绍了ContentPlaceHolder中的FindControl问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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">


您可以看到Content1和Content2.如何在ContentPlaceHolder中进行Fincontrol?
我试过了:

 TextBox t =(TextBox)Master.FindControl(" ) .FindControl(" );
TextBox txtuser =(TextBox)Page.Master.FindControl(" );
t.Text = txtuser.Text; 


母版页:

 公共 部分  class  MasterPage:System.Web.UI.MasterPage
{
    受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
    {
        txtUser.Text = " ;
    }
} 


但是会发生错误.

解决方案

如果您想尝试在母版页中找到对内容页的任何控件,请执行此操作

 //  Content1是您在母版页中的ContentPlaceHolder ID 
TextBox t =(TextBox)Content1.FindControl(" ); 


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:

TextBox t =(TextBox) Master.FindControl("Content1").FindControl("txt");
TextBox txtuser = (TextBox)Page.Master.FindControl("txtUser");
t.Text = txtuser.Text;


Masterpage:

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


But error occurs.

解决方案

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

//Content1 is your ContentPlaceHolder id in master page
TextBox t =(TextBox)Content1.FindControl("txt");


这篇关于ContentPlaceHolder中的FindControl问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆