FileUpload Doesn't Work When Nested In UpdatePanel? C# - c#

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload onchange="clickTheButton();" ID="FileUpload1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
Button 1 is outside the update panel and the javascript that gets run when a user adds a file in the upload box is this:
function clickTheButton() {
document.getElementById('<%= Button1.ClientID %>').click();
}
The problem is simple. FileUpload1.HasFile == false. I don't know why this is, but when I put it in the update panel it ceases to work.
I have seen some other threads on this. But none of them answer why this is happening, they just point to things you can download.
EDIT: Really my main reason for wanting to do this is so that I can get a ..Uploading File.. Tag to pop up while the client is uploading to the server and once it has completed, display it in a datalist. I just cant get the UpdateProgress to work.

Basically you just need to make your button do a full postback to send the file. Also make sure that you have this.Form.Enctype = "multipart/form-data"; set in your code, or you can put in that page. AsyncPostbacks don't work with files for security reasons as mentioned, without hacks. (I've never been able to get it to work).
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload onchange="clickTheButton();" ID="FileUpload1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>

For security purposes, browsers don't let you post files via javascript. Imagine if I could write a little bit a javascript to asynchronously submit the contents of your My Documents folder to my server.
So javascript-ish methods of posting the form, like the XMLHttpRequest used by the UpdatePanel, won't work.
This post describes a decent work around if you're on 3.5 SP1. http://geekswithblogs.net/ranganh/archive/2009/10/01/fileupload-in-updatepanel-asp.net-like-gmail.aspx
And this post describes a couple work arounds if you'd prefer not to use the AjaxControlToolkit. http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx

File Upload will not work with a partial post back.
So just add this line at your page load
ScriptManager.GetCurrent(this).RegisterPostBackControl(this.YourControlID);
Or use PostBackTrigger.
<Triggers>
<asp:PostBackTrigger ControlID="YourControlID" />
</Triggers>
Or You need special AsyncFileUpload control as defined in AjaxControl Toolkit.
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError"
OnClientUploadComplete="uploadComplete" runat="server"
ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" />
You can check here.

Related

How can I stream a log file to a web page using ASP WebForms

I want to use an ASP webpage to monitor a realtime log file - I have found solutions using other technologies e.g frontail, but not using ASP.
I think I need to signalr for realtime communication, and I have spent a few hours now searching for ideas and solutions without any luck. All my searches return info about the ASP and signalr logs themselves.
How can I monitor and stream a log file or multiple log files to a webpage using ASP webforms?
you might consider the UpdatePanel / Timer polling approach.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
Your repeater, datalist, listview, or other databound control here.
</ContentTemplate>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick">
</asp:Timer>
Timer1_Tick is the server side method that should rebind the databound control.

Maintaining Scroll Position on Post Back ASP.net not working

Im using an Ajax update panel with a c# multiline textbox which updates on a timer, but it keeps scrolling to the top, Ive tried setting maintainginscrollpositiononpostback to true and also tried various javascripts. The broswer im using is IE.
This is my asp code with a c# textbox on the form.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" Height="390px" TextMode="MultiLine" Width="836px"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
Im quite new to the updatepanel so maybe something is wrong there,, Thanks\
Please read this article. I think it will help you
Maintain Scroll Position after Asynchronous Postback

Why I am getting this error ,ControlID could not be found for the trigger in UpdatePanel?

It is very bad situation,sometimes in some pages of my web application i get this error:
A control with ID 'BtnSubmit' could not be found for the trigger in UpdatePanel 'UpdatePanel3'.
but if i build again and load that page it loads corectlly,but after that if i reload the page get error again,
why it is this way?How could i fix it,or find what is the detail of my problem?
in my updatepanel i dont have trigger BtnSubmit and i know the default value of 'ChildrenAsTriggers' properties if updatepanel is true,so please help me
May be you are doing this:
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnSubmit" /> //since BtnSubmit not declared any where or BtnSubmit is not accessible for UpdatePanel3
</Triggers>
<ContentTemplate>
<%-- Content--%>
</ContentTemplate>
</asp:UpdatePanel>
try adding Button(BtnSubmit) where UpdatePanel3 can access. you can add within updatepanel or outside.
<asp:Button ID="BtnSubmit" runat="server" Text="Button" />
tank you #ashwini but i found that my master page .cs file was damaged, i deleted masterpage and created it again, and now every thing is ok. it take my time because i didn't get any thing of the error message! thank to any way.

How to call server-side function from client-side in ASP.NET?

I have a web page having asp.net button control and textbox. I want a confirm message box, when someone changes the content of textbox and click on button. If user click on yes then event of button should fire other wise nothing should happen. All I want is to implement AJAX call back, but it is not working with ASP.NET button control.
Just add the following code to your aspx code
<asp:Button ID="btn1" OnClientClick="return confirm('sure?');" runat="server" Text="Button" />
You can use the ConfirmButtonExtender of the AjaxControlToolkit for this purpose,
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ConfirmButton/ConfirmButton.aspx
You can also do using Client Callback in ASP.Net, It is really nice feature in ASP.Net
Please refer to http://msdn.microsoft.com/en-us/library/ms178208.aspx. Hope it helps :)
Combining the use of OnClientClick with javascript's confirm as suggested by Pilgerstorfer Franz, with an update panel for the ajax request you mentioned...
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:TextBox ID="txtInput" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnSubmit" OnClientClick="return confirm('Are you sure you want to submit?');" runat="server" Text="Submit" />
Should give you want you need.

How to upload an image when fileupload control is under updatepanel?

How to upload an image when fileupload is under updatepanel?
I have a button say "upload" inside that update panel.
When I click that button inside the button click event I got the fileupload hasfile=false.
Please suggest if there is any way to upload image file when fileupload control is inside update panel and the button is making asyncpostback.
Thanks in advance.
you can use AJAX AsyncFileUpload control
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx
Also check this thread.
File uploading in AJAX updatepanel without full postback
It is not possible. For security reasons, the browser does not allow javascript to upload files.
There are two normal workarounds for this problem:
Perform the upload in an iFrame
Use a flash plugin for uploading
I recently applied this tool to upload files asynchronously in my web page, and it works beatifully: http://valums.com/ajax-upload/
It creates an iFrame for you automatically, and posts the frame and and sends the resulting html (or json object) to an event handler. My page that receives the uploaded file, returns a json object describing the file, e.g. filename and a unique id, so that I can link the data that is posted on the main page to the uploaded file.
For security, I store the credentials of the user uploading the file, so when the form is posted I can validate that the user that posts the form is indeed the user that uploaded the file.
I had tried as below. It is working.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Upload" runat="server" Text="Upload" OnClick="Upload_Click" /><br />
<asp:Image ID="NormalImage" runat="server" /></ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Upload" />
</Triggers>
</asp:UpdatePanel>
Reference to
http://www.c-sharpcorner.com/uploadfile/prathore/fileupload-control-in-update-panel-using-Asp-Net-ajax/
You can try this code. I tried it, it's working on my side.
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /> <br/>
<asp:Button ID="btn_Upload" runat="server" Text="Save" OnClick="btn_Upload_Click" /><br />
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_Upload" />
</Triggers>
</asp:UpdatePanel>

Categories