page.FindControl returns Null when looking for FileUpload Control

1k views Asked by At

I'm trying to upload an image using FileUpload control using a WebMethod instead of a C# Method however I cannot find the control using the page.FindControl and it returns null.

<asp:FileUpload ID="profile_pic_input" onchange="inputpic(this)" clientidmode="Static" type="file" accept="image/*"  runat="server"></asp:FileUpload>

JS:

function inputpic() {
       PageMethods.UploadPicture(onSuccess, onFailure);
}

Code Behind:

[WebMethod]
public static void UploadPicture()
{
           if (HttpContext.Current != null)
           {
                    Page page = (Page)HttpContext.Current.Handler;
                    FileUpload uploader = new FileUpload();
                    uploader = (FileUpload)page.FindControl("profile_pic_input");

                    if (uploader.HasFile)
                    {
                        Console.WriteLine("Has file!");
                    }
            }
}

It throws a NullReference Exception and when debugged, the 'uploader' object returns null and cannot find the asp control. What am I doing wrong? And yes the ASP page is allowed to use PageMethods through the ScriptManager.

0

There are 0 answers