File Upload in Update Panel within the user control not working? - asp.net

I need to use file upload in user control. user control exists in update panel .
File upload not working FileUpload HasFile always return false
<asp:UpdatePanel ID="upanel" runat="server">
<ContentTemplate>
<uc3:Assignment ID="assignment" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

Related

File upload is not working with AsyncPostBackTrigger in ASP.Net

I have one html file upload control to upload the profile picture and I'm using update panel and AsyncPostBackTrigger trigger. If I use PostBackTrigger then uploading of profile image works but If I use AsyncPostBackTrigger then uploading doesn't work.
Below is my code inline:
<asp:UpdatePanel ID="pnlZerkerBasicProfile" runat="server">
<input type="file" id="myFile" name="myFile" class="file_input_hidden" onchange="javascript:FileUploadSubmit();" style="cursor: pointer;" />
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveProfilePicture" />
</Triggers>
</asp:UpdatePanel>
Can anyone help?
This is a known issue, a full postback is required for the fileupload control to work within an updatepanel i.e. you can't use a AsyncPostBackTrigger.
Edit: Just saw this is a duplicate of FileUpload Doesn't Work When Nested In UpdatePanel? C#

ASP File Upload control not working within a previously hidden panel

I'm utilizing an ASP file upload control on a web page, and I want it hidden until the user wants to upload a file, so the update panel's visible property is false by default. When the button prompting the file upload is clicked, the upload control shows, and a file can be selected, but when the upload button is clicked, an error shows that the PostedFile property of the upload control show "Object reference not set to an instance of an object", even though a file path is visible in the conrrol. This works if the upload control is never hidden. Here is the source:
<asp:UpdatePanel ID="updUploadTestDoc" runat="server" Visible="false">
<ContentTemplate>
<asp:Panel ID="pnlUploadTestDoc" runat="server" GroupingText="Upload Test
Document">
<asp:Label ID="Label3" runat="server" SkinID="FieldLabel" Text="Select File to Upload : " /> &nbsp
<asp:FileUpload ID="uplUploadFile" runat="server" />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" text="Upload" SkinID="ConfirmButton" /> &nbsp
<asp:Button ID="btnCancelUpload" runat="server" Text="Cancel" SkinID="CancelButton" />
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
I've tried hiding/showing updUploadTestDoc, pnlUploadTestDoc and the upload control itself, all with the same results. The VB code where the error occurs is:
strAttachmentPath = pUploadControl.PostedFile.FileName
I'm using VS 2010, framework 4.0. This is my first post here, so let me know if more info is needed. Thanks.
You can't retain/assign value in FileUpload control. This is because of due to browser security reasons. The file submission is possible only on the first submission to server. You can't retain or assign a value to it.
In an UpdatePanel the same thing happens. An Ajax post submission will happen asynchronously and thus the browser won't retain the file. Read this
And the solution is to keep the fileUpload outside the UpdatePanel.
you cant use asp:fileupload inside update panel, it's kinda of issue in asp file upload ,
so get it out of the update panel and it will work perfectly

Async Triggers in ASP.Net

I have an update panel, an async trigger and a button outside the update panel. The button is wrapped with the trigger. All this is in one form. I load this form using the .load() method (jQuery). It works fine: I press the tab and the form loads into the other form. Then, when I press the button for the first time it fires the click event, but when I press it again nothing happens. Can you please help? I have tried putting the button in and out of the update panel and it's still not working. Here's my code:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName = "Click"/>
</Triggers>
<ContentTemplate>
...
If you're loading the form with jquery it may be something where you need to rebind an event, I'm not familiar with the .load method()
I assume this section of code works fine if you pull it off into a test page?

getting null path while uploading image upload in .net

I have used file upload control of asp.net but I am getting empty string while saving.my code-
<asp:FileUpload ID="fuProductLogo" runat="server" CssClass="file paddBottom5px" />
.cs code is-
if (fuProductLogo.PostedFile != null && fuProductLogo.PostedFile.ContentLength > 0)
{
...
}
but the .PstedFile and .CountLength is coming zero but the same code is working fine in another page.Please help.
There are several things to check here:
as #williem said, remove updatepanel from the form if you are using it
add enctype="application/x-www-form-urlencoded" in the form tag
Please remember to update your post after your code modifications and checks.
If the FileUpload is in an UpdatePanel it will be cleared on each ajax-postback. You can use multiple UpdatePanels around the FileUpload control and keep the FileUpload out of them. Also make sure that the button that triggers the actual upload does a real postback, not an asyncpostback. Do this by adding the button to the PostbackTrigger of it's UpdatePanel or by taking it out of the UpdatePanel.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" />
<asp:Button ID="Submit" runat="server" Text="Submit" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Submit" />
</Triggers>
</asp:UpdatePanel>
You can also use an AsyncFileUpload control from the Asp.net Ajax Toolkit, it works inside an UpdatePanel but it is a little harder to get this to work.

file upload control in update panel?

In my web application, one of my web pages contains an update panel in which I have a text box, a file upload control and a button. When the user selects a file using the file upload control and then clicks the button I am not getting any file from the file upload control. When I place a break point, the file upload control's showing a null value. Can you help me?
It's OK to have the upload control itself inside the UpdatePanel; however, the button you click to start the upload must cause a full postback.
To do this, you could either move the button outside the UpdatePanel, or use <Triggers> to force your button to cause a postback:
<UpdatePanel runat="server">
<ContentTemplate>
...
<asp:Button runat="server" ID="myButton" />
...
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="myButton" />
</Triggers>
</UpdatePanel>

Resources