FileUpload control working on second click but not first attempt of saving posted file?

风流意气都作罢 提交于 2019-12-30 02:26:06

问题


My Question

I managed to answer myself, however the same set of functionality has another problem. For some reason the first postback of the save event of the posted file hits the Ol' Object not set to an instance of an object error, but on the second attempt of uploading a file and firing my save event (converts to byte[] an stored as SQL Server BLOB) it does everything is supposed to do.

Same problem here

There is a good suggestion of using the AJAX AsyncUpload control however I am a firm believer of removing the cause and not treating the problem. I will continue down this route to best my understanding of asp.net etc.

Would there be a wizrd amongst you that could help me identify why I get "object ref not set to inst of obj" error on first postback but on second it works fine. Content page has a master page which wraps content page in an update panel. Not my decision to do this. There is also an update panel with postback triggers targeting my save event.

What are your thoughts people?


回答1:


The problem (as seen here http://forums.asp.net/t/1060363.aspx) seems to be when you use the visibility property on the surrounding panel (as it seems you are from the linked question).

The suggested workaround is to use CSS visibility instead so use this to make it invisible -

<asp:Panel ID="pnlUpload" runat="server" class="workerDetailsPanelLeft" style="display:none">

The explanation for this from the thread is

If your container is set to invisible, the upload control is not actually rendered as HTML, causing the form's enctype not to be set to enctype="multipart/form-data", causing the file upload control not to post the selected file back to the server. The workaround is either to make sure the FileUpload control is rendered to HTML (by setting its style to display:none in stead of Visible=false), or by manually setting the enctype

So another workaround would be to alter your form tag to this

<form id="form1" enctype="multipart/form-data" runat="server">

I think either one of those should solve your problem.




回答2:


You could do the same thing via code on Page_Load event.. Enter this code and it will solve the issue.

Page.Form.Attributes.Add("enctype", "multipart/form-data");


来源:https://stackoverflow.com/questions/11575716/fileupload-control-working-on-second-click-but-not-first-attempt-of-saving-poste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!