ASP.NET从代码隐藏中的ID获取FileUpload对象

时间:2017-03-27 17:02:53

标签: c# asp.net

我们说我的fileUpload id是" fuID"并且它已在我的ASCX文件中定义。

如何使用其Id获取我的代码隐藏(cs)中的FileUpload对象?

如:

    public voiattachA_Click(object sender, EventArgs e)
    {
        string fuid = hiddenItemIndex; // which is the "fuID" string
        FileUpload fu; // = something using its ID
    }

感谢advence

1 个答案:

答案 0 :(得分:0)

我假设您的代码隐藏在使用该用户控件的页面上,该控件包含您想要的文件上传控件

你可以通过以下方式获得:

public voiattachA_Click(object sender, EventArgs e)
{
    string fuid = hiddenItemIndex; // which is the "fuID" string
    // get instance of the custom control by id
    var objTestControl = (UserControlType)Page.FindControl("yourUserControlID");
    // find the file upload control in the custom control by id
    FileUpload fu = objTestControl.FindControl(fuid) as FileUpload;
}