I have an UpdateProgress control to activate on a postback of a LinkButton control in UpdatePanel. The LinkButton_click event is performing a Response.Redirect to pull up a Downloading. The Download comes up and the UpdateProgress bar is spinning on the
page with the button. Either cancel downloading the UpdateProgress image is still there spinning.
1- How can I make the UpdateProgress bar to finish and go away?
2- How can Freez page while spinning loding?
My code is
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Downloads")
{
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow selectedRow = GridView1.Rows[index];
string cid = selectedRow.Cells[1].Text;
DownloadFile();
}
public void DownloadFile()
{
string directoryString = Session.SessionID;
DirectoryInfo folder = Directory.CreateDirectory(directoryString);
foreach (FileInfo file in folder.GetFiles())
{ file.Delete(); }
..
.
.
string ss = GridView2.Rows[0].Cells[1].Text;
string s = GridView2.Rows[0].Cells[2].Text;
string filename_path = "D:\\VLS\\" + s + "\\" + ss;
if (File.Exists(filename_path))
{
lberror.Visible = false;
string filename = filename_path;
System.Threading.Thread.Sleep(1000);
Response.Redirect("downloading.aspx?folder=" + directoryString + "&file=" + fname + "&DownloadName=" + ss);
}
else
{
lberror.Visible = true;
lberror.Text = "File does not exist";
}
}
It is not a good practice to use Response.Write or server.execute from within an ASP.NET page in general. As UpdatePanels work by intercepting the page rendering process you are recieving the errors.
You can put the code to generate the data in an HTTPHandler or you can make the button a PostBackTrigger.
Because of the way updatepanels manage the DOM elements within them, you cannot use Response.Write() with them.
If redirecting to other page then UpdateProcess image is not shwon
This will surely work, because I created this scenario locally & it's successfull.
The problem with your code is, the request goes to server -- and the download code ends/clears that response to pop-up the download dialog box. If you put a breakpoint anywhere in parent page's, "PageRequestManager-endrequest event handler", you would notice
it never gets a chance to run, after you click download.
Hence the UpdateProgress kinda hangs on the parent page even after the download pop-up box had shown up & the user has chosen to either save/open/cancel.
The solution which worked for me was to -- redirect to the download page at the end of request through javascript -- i.e. through PageRequestManager-endrequest event handler.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<script type="text/javascript" language="javascript">
var DownloadBtn;
var DownloadFile;
function SaveTheDownloadBtn(MyArgs) {
var index = MyArgs.indexOf(',');
DownloadBtn = MyArgs.substring(0, index);
DownloadFile = "Download.aspx?filename="+MyArgs.substring(index+1);
}
function GoToDownload(strFilename) {
window.location.href = strFilename;
}
var prm = Sys.WebForms.PageRequestManager.getInstance();
var postBackElement;
prm.add_endRequest(EndRequest);
prm.add_initializeRequest(InitializeRequest);
prm.add_beginRequest(beginRequest);
function InitializeRequest(sender, args) {
postBackElement = args.get_postBackElement();
}
function EndRequest(sender, args) {
if (DownloadBtn == postBackElement.id) {
GoToDownload(DownloadFile);
}
}
function beginRequest(sender, args) {
if (DownloadBtn == postBackElement.id) {
var tabContainer = $get(postBackElement.id);
var _updateProgressDiv = $get('<%= updProgressTab.ClientID %>');
var tabContainerwBounds = Sys.UI.DomElement.getBounds(tabContainer);
var updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);
// center of download button
var x = tabContainerwBounds.x + Math.round(tabContainerwBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2)-37;
var y = tabContainerwBounds.y + Math.round(tabContainerwBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2)-15;
Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);
}
}
</script>
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:GridView ID="gvFiles" runat="server" GridLines="None"
AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false"
PageSize="10" OnRowCreated="gvFiles_RowCreated" CellPadding="10"
CellSpacing="0" Width="900px" onrowcommand="gvFiles_RowCommand"
onrowdatabound="gvFiles_RowDataBound" EnableViewState="true"
DataKeyNames="fileID" >
<AlternatingRowStyle BackColor="aliceBlue" />
<HeaderStyle HorizontalAlign="Left" BackColor="Gray" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Right" />
<Columns>
<asp:BoundField DataField="fileID" HeaderText="fileID" />
<asp:BoundField DataField="filePath" HeaderText="filePath" />
<asp:BoundField DataField="fileName" HeaderText="fileName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn1" runat="server" Text="Download" CommandName="abc1" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="updProgressTab" AssociatedUpdatePanelID="updatePanel" runat="server" >
<ProgressTemplate>
<div style="background:White">
<asp:Image ID="imgLoading" runat="server" ImageUrl="simple.gif" Width="34px" />Loading...
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</asp:Content>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "abc1")
{
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow selectedRow = gvFiles.Rows[index];
Button btn1 = (Button)selectedRow.FindControl("btn1");
System.Threading.Thread.Sleep(3000);
//Response.Redirect("Download.aspx?filename=" + selectedRow.Cells[1].Text); //This won't work because the the PageRequestManager endrequest function
//never gets a chance to execute. Use JS to redirect
}
}
protected void gvFiles_RowCreated(object sender, GridViewRowEventArgs e)
{
}
protected void gvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn1 = (Button)e.Row.FindControl("btn1");
btn1.Attributes.Add("onclick", "SaveTheDownloadBtn('" + btn1.ClientID + "," + e.Row.Cells[1].Text + "');");
}
}
private void BindGrid()
{
gvFiles.DataSource = FileFields.GetMyFiles();
gvFiles.DataBind();
}
I added the delay in the Grid's RowCommand handler and when all the server logic is executed & asyn postback is about to be all done (ie when the control comes in endRequest event handler), I redirect it to download.aspx
I added some extra logic to make sure that the file download pops-up only when the download button is hit & no other button in that page.
http://simpledotnetsolutions.wordpress.com/
2011 Microsoft Community Contributor Award Recipient
Marked as answer by shaair on Oct 06, 2010 05:03 AM
One more thing that you can check for in Browser setting is "Play Animations in Web Page*" under Advanced tab. Enable this option and you could find the Animation working good.
shaair
Member
401 Points
238 Posts
UpdateProgress with a Response.Redirect
Oct 03, 2010 02:43 PM|LINK
I have an UpdateProgress control to activate on a postback of a LinkButton control in UpdatePanel. The LinkButton_click event is performing a Response.Redirect to pull up a Downloading. The Download comes up and the UpdateProgress bar is spinning on the page with the button. Either cancel downloading the UpdateProgress image is still there spinning.
1- How can I make the UpdateProgress bar to finish and go away?
2- How can Freez page while spinning loding?
My code is
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Downloads") { int index = int.Parse(e.CommandArgument.ToString()); GridViewRow selectedRow = GridView1.Rows[index]; string cid = selectedRow.Cells[1].Text; DownloadFile(); }public void DownloadFile() { string directoryString = Session.SessionID; DirectoryInfo folder = Directory.CreateDirectory(directoryString); foreach (FileInfo file in folder.GetFiles()) { file.Delete(); } .. . . string ss = GridView2.Rows[0].Cells[1].Text; string s = GridView2.Rows[0].Cells[2].Text; string filename_path = "D:\\VLS\\" + s + "\\" + ss; if (File.Exists(filename_path)) { lberror.Visible = false; string filename = filename_path; System.Threading.Thread.Sleep(1000); Response.Redirect("downloading.aspx?folder=" + directoryString + "&file=" + fname + "&DownloadName=" + ss); } else { lberror.Visible = true; lberror.Text = "File does not exist"; } }Thanks,
chetan.sarod...
All-Star
65619 Points
11118 Posts
Re: UpdateProgress with a Response.Redirect
Oct 04, 2010 03:16 AM|LINK
It is not a good practice to use Response.Write or server.execute from within an ASP.NET page in general. As UpdatePanels work by intercepting the page rendering process you are recieving the errors.
You can put the code to generate the data in an HTTPHandler or you can make the button a PostBackTrigger.
Because of the way updatepanels manage the DOM elements within them, you cannot use Response.Write() with them.
If redirecting to other page then UpdateProcess image is not shwon
See http://forums.asp.net/t/1254076.aspx
1. You can hide the UpdateProgressBar in EndRequestHandler
Check this link
http://weblogs.asp.net/alessandro/archive/2007/09/30/howto-show-updateprogress-conditionally-for-specific-controls-triggering-an-async-postback.aspx
2. http://dhavalupadhyaya.wordpress.com/2008/06/04/how-to-block-freeze-webpage-while-it-is-being-processed-in-net-20-using-ajax/
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
shaair
Member
401 Points
238 Posts
Re: UpdateProgress with a Response.Redirect
Oct 04, 2010 10:14 AM|LINK
thanx for reply and solved Freez probelm. but spinning loader still:( plz help me according to update panel.
Jerry Weng -...
All-Star
29527 Points
3488 Posts
Re: UpdateProgress with a Response.Redirect
Oct 05, 2010 02:12 AM|LINK
Hi,
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <script type="text/javascript"> function endProgress() { var progress = $find("UpdateProgress1"); progress._pageRequestManager.abortPostBack(); } </script> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:UpdateProgress ID="UpdateProgress1" runat="server"> <ProgressTemplate> loading... </ProgressTemplate> </asp:UpdateProgress> <asp:Button ID="Button1" runat="server" Text="AsyncPostBack" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="Cancel" OnClientClick="endProgress();return false;" /> </ContentTemplate> </asp:UpdatePanel>shaair
Member
401 Points
238 Posts
Re: UpdateProgress with a Response.Redirect
Oct 05, 2010 11:28 AM|LINK
No dear problem not going to solve:(
DotNetSeeker
Contributor
3169 Points
589 Posts
Re: UpdateProgress with a Response.Redirect
Oct 05, 2010 12:59 PM|LINK
This will surely work, because I created this scenario locally & it's successfull.
The problem with your code is, the request goes to server -- and the download code ends/clears that response to pop-up the download dialog box. If you put a breakpoint anywhere in parent page's, "PageRequestManager-endrequest event handler", you would notice it never gets a chance to run, after you click download.
Hence the UpdateProgress kinda hangs on the parent page even after the download pop-up box had shown up & the user has chosen to either save/open/cancel.
The solution which worked for me was to -- redirect to the download page at the end of request through javascript -- i.e. through PageRequestManager-endrequest event handler.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <script type="text/javascript" language="javascript"> var DownloadBtn; var DownloadFile; function SaveTheDownloadBtn(MyArgs) { var index = MyArgs.indexOf(','); DownloadBtn = MyArgs.substring(0, index); DownloadFile = "Download.aspx?filename="+MyArgs.substring(index+1); } function GoToDownload(strFilename) { window.location.href = strFilename; } var prm = Sys.WebForms.PageRequestManager.getInstance(); var postBackElement; prm.add_endRequest(EndRequest); prm.add_initializeRequest(InitializeRequest); prm.add_beginRequest(beginRequest); function InitializeRequest(sender, args) { postBackElement = args.get_postBackElement(); } function EndRequest(sender, args) { if (DownloadBtn == postBackElement.id) { GoToDownload(DownloadFile); } } function beginRequest(sender, args) { if (DownloadBtn == postBackElement.id) { var tabContainer = $get(postBackElement.id); var _updateProgressDiv = $get('<%= updProgressTab.ClientID %>'); var tabContainerwBounds = Sys.UI.DomElement.getBounds(tabContainer); var updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv); // center of download button var x = tabContainerwBounds.x + Math.round(tabContainerwBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2)-37; var y = tabContainerwBounds.y + Math.round(tabContainerwBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2)-15; Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y); } } </script> <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional" > <ContentTemplate> <asp:GridView ID="gvFiles" runat="server" GridLines="None" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false" PageSize="10" OnRowCreated="gvFiles_RowCreated" CellPadding="10" CellSpacing="0" Width="900px" onrowcommand="gvFiles_RowCommand" onrowdatabound="gvFiles_RowDataBound" EnableViewState="true" DataKeyNames="fileID" > <AlternatingRowStyle BackColor="aliceBlue" /> <HeaderStyle HorizontalAlign="Left" BackColor="Gray" /> <RowStyle HorizontalAlign="Center" /> <PagerStyle HorizontalAlign="Right" /> <Columns> <asp:BoundField DataField="fileID" HeaderText="fileID" /> <asp:BoundField DataField="filePath" HeaderText="filePath" /> <asp:BoundField DataField="fileName" HeaderText="fileName" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="btn1" runat="server" Text="Download" CommandName="abc1" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> </ContentTemplate> </asp:UpdatePanel> <asp:UpdateProgress ID="updProgressTab" AssociatedUpdatePanelID="updatePanel" runat="server" > <ProgressTemplate> <div style="background:White"> <asp:Image ID="imgLoading" runat="server" ImageUrl="simple.gif" Width="34px" />Loading... </div> </ProgressTemplate> </asp:UpdateProgress> </asp:Content>protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "abc1") { int index = int.Parse(e.CommandArgument.ToString()); GridViewRow selectedRow = gvFiles.Rows[index]; Button btn1 = (Button)selectedRow.FindControl("btn1"); System.Threading.Thread.Sleep(3000); //Response.Redirect("Download.aspx?filename=" + selectedRow.Cells[1].Text); //This won't work because the the PageRequestManager endrequest function //never gets a chance to execute. Use JS to redirect } } protected void gvFiles_RowCreated(object sender, GridViewRowEventArgs e) { } protected void gvFiles_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Button btn1 = (Button)e.Row.FindControl("btn1"); btn1.Attributes.Add("onclick", "SaveTheDownloadBtn('" + btn1.ClientID + "," + e.Row.Cells[1].Text + "');"); } } private void BindGrid() { gvFiles.DataSource = FileFields.GetMyFiles(); gvFiles.DataBind(); }I added the delay in the Grid's RowCommand handler and when all the server logic is executed & asyn postback is about to be all done (ie when the control comes in endRequest event handler), I redirect it to download.aspx
I added some extra logic to make sure that the file download pops-up only when the download button is hit & no other button in that page.
2011 Microsoft Community Contributor Award Recipient
shaair
Member
401 Points
238 Posts
Re: UpdateProgress with a Response.Redirect
Oct 06, 2010 05:00 AM|LINK
wow its great:) bundle ov thanx. but i did it another alternate way.
i bind javascript to download at row_databind() and the it will works.
Saran0006
Member
16 Points
3 Posts
Re: UpdateProgress with a Response.Redirect
Jan 13, 2011 02:36 AM|LINK
Hi,
One more thing that you can check for in Browser setting is "Play Animations in Web Page*" under Advanced tab. Enable this option and you could find the Animation working good.
Update progress freeze
www.saravanan-p.blogspot.com