UpdatePanel quits responding after Response.TransmitFile - asp.net

I have a GridView in an UpdatePanel that shows a list of files. One of the columns in the GridView is a button that opens the listed PDF file.
<asp:UpdatePanel runat="server" ID="upPurchaseOrder">
<ContentTemplate>
<asp:GridView ID="gvPurachaseOrder" runat="server" AutoGenerateColumns="False" DataSourceID="odsPurchaseOrders"
EnableModelValidation="True" DataKeyNames="PurchaseOrderID,PromotionID" onrowcreated="gvPurachaseOrder_RowCreated">
<Columns>
<%--BoundFields omitted--%>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnPurchaseOrderOpen" runat="server" Text="Open" CommandName="Open"
CommandArgument='<%# Eval("FilePath") %>' oncommand="btnPurchaseOrderOpen_Command" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
In the RowCreated event I register btnPurchaseOrderOpen as a PostBack control, so it causes a full postback.
In the button event handler, I open the file with the Response.TransmitFile method.
Response.Clear();
Response.ContentType = GetConentTypeForFile(fi.Extension);
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", fi.Name));
Response.TransmitFile(fi.FullName);
Response.End();
My problem is that after the file is opened (it opens correctly) the UpdatePanel stops updating. I can click the other buttons and they never post back synchronously or asynchronously. I have a feeling the problem is related to the response, but I don't know how to fix it.

You are doing a full post back but you are not returning any new page to the server since you are only doing transmitfile and then response.end. It's basically a dead end for your page. The way you may want to get around this is to do the full post back in an iframe or simply create a direct link to the download file, even if that might be a ashx handler where you can continue to have similar logic to what you are already using.

Related

Where do I find the commandname handler for asp:linkbutton

Trying to debug a aspx script. Don't know any asp.net.
I've come across this piece of code:
<asp:LinkButton
ID="EditButton"
runat="server"
CausesValidation="False"
CommandName="Edit"
Text="Edit"
CssClass="LightBlue">
</asp:LinkButton>
From this site:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.command%28v=vs.110%29.aspx
I see that the method to look for is LinkButton_Command. I have done a search in all the files and not found this method.
This link is doing something, so it's not ignored. Could this method have a different name and if so how do I find what it is?
I have changed the Text attribute and that change is there when I run it, so I have the right piece of code.
I've also changed the CommandName attribute and the link stops working, so something somewhere is handling it.
There are two more linkbuttons in the code immediately after it:
<asp:LinkButton
ID="DeleteButton"
runat="server"
CausesValidation="False"
CommandName="Delete"
Text="Delete"
CssClass="LightBlue"
OnClick="DeleteButton_Click">
</asp:LinkButton>
<asp:LinkButton
ID="NewButton"
runat="server"
CausesValidation="False"
CommandName="New"
Text="New"
CssClass="LightBlue">
</asp:LinkButton>
I notice that the deletebutton has an onclick attribute, and I can find that method within the same script, but nothing obvious near that handler for the other two.
EDIT:
Another way of getting the answer that I want might be to ask the question:
What are all the different ways to add a click handler to a asp:linkbutton?
EDIT:
Don't know if it helps, but the link button is inside the following structure:
<ajax:UpdatePanel...>
<ContentTemplate>
<asp:FormView
id="FormView1"
runat="server"
OnItemDeleted="FormView1_ItemDeleted"
DataKeyNames="Id"
OnDataBound="FormView1_DataBound"
OnItemUpdated="FormView1_ItemUpdated"
OnItemUpdating="FormView1_ItemUpdating"
OnItemInserted="FormView1_ItemInserted"
OnItemInserting="FormView1_ItemInserting"
DefaultMode="Insert"
DataSourceID="odsLogEntryForm"
>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="EditButton"...
The difference with the example from the site you posted is that they used the OnCommand="LinkButton_Command" to subscribe the handler to the event. In your example this is not the case. This can mean 2 things:
- The event is not handled
- The event is subscribed to in code, probably in the code behind file of the aspx/ascx file (.aspx.cs). Look for the following code:
EditButton.Command += <name of the function to handle the command event>;
EditButton is the name of your LinkButton.
Command is the name of the event to handle.
<name of the function to handle the command event> is a function which the programmer created. He/She could have named it anything.
After seeing your edit:
The event is handled by the encapsulating FormView. A FormView will handle command events of a inner buttons. Since the CommandName of the LinkButtons is set to "Delete" and "New" is will be handled by the corresponding events in the FormView.
see: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview_events(v=vs.110).aspx for the available events of a FormVIew in this case the events with the name "Item" apply.

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

ASP.NET how best to add a delete confirmation on a gridview with EntityDataSource

I have a .NET4 web application using the Entity Framework
In one of my pages I have a gridview bound to an entity data source. Within the Gridview definition I have
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
and the EntityDataSource is defined
<asp:EntityDataSource ID="eds_timesheets" runat="server" ConnectionString="name=TIME_ENTRY_DB"
DefaultContainerName="TIME_ENTRY_DB" EnableDelete="True" EnableFlattening="False"
EnableUpdate="True" EntitySetName="TIMESHEETs" Include="USER, ACTIVITY, PROJECT"
EntityTypeFilter="TIMESHEET">
</asp:EntityDataSource>
Everything works as expected, however I now want to put a confirmation of delete in place in case of accidental pressing.
I have tried placing code on the row command of the gridview wwhich would register a javascript alert window however it appears that at this point the EntityDataSource has already carried out its delete.
There is no OnClientClick for a gridview command field to place a small javascript snippet.
Has anyone encoutnered and subsequently solved this issue? Is it easier to have a link button and handle the delete of the Entity data source myself?
Use a TemplateField, and then put a Button in it with the CommandName="Delete". Then you can use the OnClientClick property to call your javascript confirmation.
Something like the following (can use Button, ImageButton, or LinkButton):
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="DeleteButton" runat="server" text="Delete"
CommandName="Delete" OnClientClick="return confirm('Are you sure?');" ></asp:Button>
</ItemTemplate>
</asp:TemplateField>
Of course, you would not want to show the delete button in the CommandField.

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.

Postback a linkbutton in a grid that's under an UpdatePanel

as my title says...
How do you make a LinkButton fire a POSTBACK (not ASYNCPOSTBACK) which is inside a GridView and is under an UpdatePanel?
My Scenario is this,
I have a grid. say table A, which populates a Linkbuttons with link to do Server.Transfer calls from Page1 to Page2.
I have a good reason why i am using a Server.Transfer because of previous page referencing methods and Response.Redirect doesnt fit at all.
normally it would work if i add the grid as a Postback trigger in the UpdatePanel like so
<Triggers><asp:PostBackTrigger ControlID="gvitem" /></Triggers>
but since I have another control inside the grid that needs to be do an AsyncPostback, that would not work also,
all that's lacking is have this line of code, do postback.
<asp:TemplateField HeaderText="Description" SortExpression="ShortDesc">
<ItemTemplate>
<asp:LinkButton ID="btndesc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ShortDesc")%>' CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Key") %>' />
</ItemTemplate>
</asp:TemplateField>
anyone have an idea?
Find btndesc on grid.ItemDataBound and register it as a PostBackTrigger.

Resources