Nick Rolando
Nick Rolando

Reputation: 26177

Strange __EVENTTARGET behavior, possible bug?

Herro. I have a strange problem that I am dealing with that involves two buttons, an HTML (client-side "Export to Excel") and an ASP (server-side "Go") button:

enter image description here

Here is the html for the two buttons:

<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGoClick" Width="35px" />
<input id="btnExport" type="button" value="Export to Excel" disabled="disabled" onclick="btnExClick(this)" onmouseover="return doHover(this)" onmouseout="this.style.backgroundColor='';" style="width: 125px" />

Basically the user selects in the drop down, what loans to load, and clicks "Go", which queries the database and loads the data grid. When a datagrid is loaded the "Export to Excel" button becomes enabled and will turn to and from the color green when the user hovers the mouse over and off. When clicked, it fires and event that just calls __doPostBack(btnEx.id, '');

Edit Per ShadowWizard's Request: Here is my btnExClick():

function btnExClick(btn)
{
    document.forms[0].target = "_blank";  //newly added line from shadowwizard
    __doPostBack(btn.id, '');
}

In my code-behind, I have the following code:

protected void Page_Load(object sender, EventArgs e)
    {
        .....

        if (Request.Form["__EVENTTARGET"] == "btnExport")
        {
            this.ExportExcel();
        }
    }

    protected void ExportExcel()
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=Optoma Loaner Report.xls");
        Response.Charset = "";
        this.EnableViewState = false;

        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

        dgResults.RenderControl(htw);

        Response.Write(sw.ToString());
        Response.End();
    }


This all works fine, and I get a nice looking Excel sheet. The only problem is after I "Export to Excel", my "Go" button no longer works. It just performs a "postback" with an __EVENTTARGET still = "btnExport", so it attempts to export to excel again.. It doesn't even go to its server-side onclick event handler! Does anyone know why this is happening?
I'm assuming it has something to do with the way I export my datagrid to an excel sheet, because when I comment out this.ExportExcel();, it continues to work fine (although the "Export to Excel" button goes back to being disabled after I click it, and I'm not sure why. It doesn't normally do that).

Upvotes: 1

Views: 2903

Answers (5)

S. Bills
S. Bills

Reputation: 1

I had the same issue--an ASP button to get data and an html button to export it to Excel. My solution was to call a JavaScript from the ASP button "OnClientClick" to clear __EVENTTARGET value:

OnClientClick="clearEVENTTARGET(); return true;"

function clearEVENTTARGET() {
        this.<form name here>.__EVENTTARGET.value = '';
    };

Upvotes: 0

maddog2k
maddog2k

Reputation: 31

I had this exact issue.

The fix is to set UseSubmitBehavior="false" on your btnGo button.

UseSubmitBehavior="false" causes the button to use .NET's postback methodology which will set __EVENTTARGET to "btnGo" when the button is clicked. Without it, the button is using the browser's default methodology for submit buttons which leaves __EVENTTARGET unchanged and therefore resubmits the previous value of "btnExport".

Upvotes: 3

Nick Rolando
Nick Rolando

Reputation: 26177

Not getting too many responses, maybe I can spark some up. I think the reason this might be happening is because the response isn't being returned to the "browser", so to speak, but to an excel sheet. I think that's why I'm not seeing my html controls go back to default state like they normally should on a postback. So maybe when I try to do another postback its still feeding it the same postback as before.. but how could I prevent that..

Well, the whole reason I use an html button is to do a fancy color change. If I use an ASP:Button onclick handler to do my export, it works fine and I get no problems. I'm not sure where the problem lies, between using an html button and __doPostBack() and an asp button onclick event to do the same thing.

Upvotes: 0

ChrisLively
ChrisLively

Reputation: 88072

Usually, you tie the excel generator to a generic handler (.ashx file). The handlers entire purpose is to take a couple parameters and emit the excel file.

In your page code you just tie the button click to pull up the .ashx file.

Upvotes: 0

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

This probably happens because you Clear the response and Ending it.. I would try to open the Excel in new browser window by changing the form target before submitting the form.

How to do that? By adding such line to btnExClick function:

document.forms[0].target = "_blank";

Hopefully this will cause the Excel to open in new window and leave the original window intact.

Upvotes: 1

Related Questions