Forced Return of Asp.Net

Note the following click event ...

 Protected Sub btnDownloadEmpl_Click (ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownloadEmpl.Click
        Dim emplTable As DataTable = SiteAccess.DownloadEmployee_H ()
        Dim d As String = Format (Date.Now, "d")
        Dim ad () As String = d.Split ("/")
        Dim fd As String = ad (0) & ad (1)
        Dim fn As String = "E_" & fd & ".csv"
        Response.ContentType = "text / csv"
        Response.AddHeader ("Content-Disposition", "attachment; filename =" & fn)
        CreateCSVFile (emplTable, Response.Output)
        Response.Flush ()
        Response.End ()
        lblEmpl.Visible = True
    End sub

This code just exports data from datatable to csv file. The problem is here: lblEmpl.Visible = true never hits, because this code does not cause a postback to the server. Even if I put the line of code lblEmpl.Visible = true at the top of the click event, the line will execute fine, but the page never refreshes. How can i fix this?

+3
source share
4 answers

This line:

lblEmpl.Visible = True

Never hits because this line:

Response.End()

Throws out a ThreadAbortException

, - HttpHandler "" . ( . /.)

IHttpHandler. .

. , , :

public class CensusHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string fileName = String.Format(
            CultureInfo.CurrentUICulture,
            "E_{0:00}{1:00}.csv",
            DateTime.Today.Month,
            DateTime.Today.Day
            );

        context.Response.ContentType = "text/csv";
        context.Response.AddHeader(
            "Content-Disposition", String.Format(null, "attachment; filename={0}", fileName)
            );

        //Dump the CSV content to context.Response

        context.Response.Flush();
    }

    public bool IsReusable { get { return false; } }
}

, onclick javascript :

<asp:Button ID="Clickety" runat="server" Text="Click Me!" OnClick="Clickety_Click"
    OnClientClick="window.open('Handler.ashx', 'Download');" />

OnClick . javascript onclick (OnClientClick) HttpHandler.

+4

CSV , . , , .

, , . - / . CSV , .

+1

, . , , -

  • CSV, page.cs dataID .

    Protected Sub onLoad()
        Dim recordID As Integer = Request.Querystring("dID")
        Dim emplTable As DataTable = Nothing 
        Select Case recordID 
          case 1: emplTable = SiteAccess.DownloadEmployee_H() 
          case 2: emplTable = SiteAccess.DownloadManagers() 
        End Select 
        Response.Clear()
        Response.ContentType = "text/csv"
        Response.AddHeader("Content-Disposition", "attachment; filename=" & fn)
        CreateCSVFile(emplTable, Response.Output)
        Response.Flush()
        Response.End() 
        lblEmpl.Visible = True
    End Sub
    
  • onClientClick script .

    function fnPopUpCSV(expType)
    {        
    window.open('/popUpPage.aspx?dID=' + expType,'CSVwindow', 
    'width=300,height=200,menubar=yes,status=yes,
    location=yes,toolbar=yes,scrollbars=yes');
    }
    
    <asp:Button ID="btnGenEmplCSV" runat="server" Text="Generate Employee CSV"
    onClientClick="javascript:return fnPopUpCSV(1);" />
    
    <asp:Button ID="btnGenMgrCSV" runat="server" Text="Generate Manager CSV"
    onClientClick="javascript:return fnPopUpCSV(2);" />
    
+1

_Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim emplTable As DataTable = SiteAccess.DownloadEmployee_H()
    Dim d As String = Format(Date.Now, "d")
    Dim ad() As String = d.Split("/")
    Dim fd As String = ad(0) & ad(1)
    Dim fn As String = "E_" & fd & ".csv"
    Response.ContentType = "text/csv"
    Response.AddHeader("Content-Disposition", "attachment; filename=" & fn)
    CreateCSVFile(emplTable, Response.Output)
    Response.Flush()
    Response.End()
End Sub

lblEmpl script, , csv.

var csvPageJS = string.Format("window.open ('{0}','mywindow');", ResolveUrl("~/MyCSVPage.aspx"));
  ClientScript.RegisterStartupScript(typeof(Page), "popup", csvPageJS, true);
0

Source: https://habr.com/ru/post/1745129/


All Articles