Asp.net 当页面位于母版页内时,在代码隐藏中获取fileupload id

Asp.net 当页面位于母版页内时,在代码隐藏中获取fileupload id,asp.net,Asp.net,我想知道,当整个内容都在ContentPlaceholder 1中时,如何访问fileupload控件id。我曾尝试使用FindControlRecursive(控件根,字符串id)方法,并将其称为FindControlRecursive(ContentPlaceholder 1,fileupload1)但是它不起作用。因此,请告诉我如何找到fileupload1 id您可以使用母版页控件的FindControl方法在内容页面中找到FileUpload控件 protected void Page

我想知道,当整个内容都在ContentPlaceholder 1中时,如何访问fileupload控件id。我曾尝试使用FindControlRecursive(控件根,字符串id)方法,并将其称为FindControlRecursive(ContentPlaceholder 1,fileupload1)但是它不起作用。因此,请告诉我如何找到fileupload1 id

您可以使用
母版页
控件的
FindControl
方法在
内容
页面中找到
FileUpload
控件

protected void Page_Load(object sender, System.EventArgs e)
{
    ContentPlaceHolder content = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;    
    FileUpload fu = content.FindControl("FileUpload1") as FileUpload;
    //...
}

您的fileupload控件添加到了哪里?我有一个id为fileuplaod1的fileupload控件,当我在codebehind文件中定义它时,就像fileupload fileupload1一样,它没有母版页。它工作正常,但是将页面添加到母版页会产生问题,它找不到它。它会在page.Master.FindControl(“contentplaceholder 1”)上给我一个错误;无法从system.web.ui.control隐式转换为system.web.ui.control.ContentPlaceHolder您需要转换为ContentPlaceHolder,我已编辑了示例。