ASP File Upload control not working within a previously hidden panel - asp.net

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

Related

UpdatePanel full postback on production server but async on local server

I have a simple ASP.NET 4.0 web form which has an UpdatePanel with a button and TextBox inside. There is nothing unusual about it (it is not inside a repeater and it is not inside a dynamic control).
Asynchronous postback works fine on my test server which is Windows Server 2003.
However, when I copy to my web hosting company (smarterasp.net, Server 2012) it does a full postback. I tried all of the settings available in the hosting control panel but nothing worked (eg. HTTP compression, Integrated mode, output caching).
Are there any usual causes for why this happens on different servers?
<asp:UpdatePanel ID="panSlug" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="form-group">
<asp:Label ID="lblSlug" runat="server" AssociatedControlID="txtSlug" Text="Slug"></asp:Label>
<div class="input-group">
<asp:TextBox ID="txtSlug" runat="server" CssClass="form-control" MaxLength="100"></asp:TextBox>
<span class="input-group-btn">
<asp:Button ID="btnSlugSuggest" runat="server" CssClass="btn btn-default" OnClick="btnSlugSuggest_Click" Text="Suggest" />
</span>
</div>
<asp:RequiredFieldValidator ID="valSlug" runat="server" ControlToValidate="txtSlug" Display="None" ErrorMessage="Slug is a required field" ValidationGroup="Product"></asp:RequiredFieldValidator>
<p class="help-block">Slug is the URL of the product (no spaces or non alpha numeric characters)</p>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Looking at the output HTML on the local server the button renders as input type button with an onclick event, whereas on the live server it renders as a submit button with no onclick event.
In Developer Tools console two errors are shown: "ASP.NET Ajax client-side framework failed to load" and "Sys is not defined".
It turns out that I had a page route set in global.asax that was preventing the axd files from loading.
I was missing routes.Ignore("{resource}.axd/{*pathInfo}"); from global.asax.
Hopefully this answer will help someone else as another possible solution to problems with UpdatePanels.
Add this to the UpdatePanel:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSlugSuggest" />
</Triggers>

asp.net how to access route data from an iframe

I have a basic asp.net page with an image upload loaded through an iframe:
<asp:Panel runat="server">
<asp:TextBox runat="server" id="imgCaption" CssClass="imgCaption" PlaceHolder="caption(optional)" />
<asp:FileUpload runat="server" ID="programmImages" Width="200px" CssClass="offScreen" />
<asp:Button runat="server" id="hiddenImageUpload" OnClick="UploadImage" CssClass="insButton" Text="Upload" />
</asp:Panel>
the image upload depends on the route data in order to upload but whenever i try to upload the image i get this error:
Object reference not set to an instance of an object
on this line:
var ProgName = Page.RouteData.Values["prog"].ToString();
this line works fine in other pages.
so how can i retrieve the route data through the iframe.
Thanks
i figured out the problem, the iframe is a seperate page than the page it resides in so i had to pass the data as querystrings and it worked fine.

AjaxControlToolkit CalendarExtender Control not working in my website

I' using the AjaxControlToolkit CalendarExtender in my web site but after uploading it does how show the calendar control. I have registered the control on the page itself. The page postback when I click the image on which I applied the control.
What can be the reason of this?
Check whether you added ToolkitscriptManager in your page, which is in AjaxToolkit
If you are using an ImageButton to show the calendar,
then use ajax control toolkit and put these controls
<asp:ToolkitScriptManager runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="calIcon.jpg" />
<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBox1"
PopupButtonID="ImageButton1" runat="server">
</asp:CalendarExtender>
If you are still getting the page postback when clicking the ImageButton, you can add onClientClick='return false;' to the imageButton
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="calIcon.jpg"
OnClientClick="return false;" />
Drag and place Script manager at top of the ajax control or at top of the form.
As you said that ajax calender works fine on local but when you uploaded it it started creating problems following are the workarounds give them a try.
Check that whether you uploaded the ajax toolkit dll on the server or not.
secondly make sure that you uploaded updated web.config file.
let me know if its helpful otherwise i will try finding other workarounds

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