我尝试使用FileMethod而不是C#方法使用FileUpload控件上传图像但是我无法使用page.FindControl找到控件,它返回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);
}
代码背后:
[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!");
}
}
}
抛出NullReference异常,并在调试时,&#39;上传器&#39; object返回null并且找不到asp控件。我究竟做错了什么?是的,ASP页面允许通过ScriptManager使用PageMethods。