0

I have a report that feeds the user a TDF text file. Two filters are selected and two fields are filled out before the user can submit, with a friendly error message appearing if the "download" button is clicked without them, requesting the user complete these fields before continuing. My testing departments wants this error to go away upon successful download of the file. The response object stops the page from posting back so the error message can disappear.

To attempt to work around this I've added an extra, invisible button wired up to the code creating the TDF, and attached a Javascript click event to the visible button.

 <asp:Button ID="btnSubmit" runat="server" Text="" Style="visibility:hidden;" /><asp:Button ID="btnDownload" runat="server" Text="Download TDF" OnClientClick="document.getElementById('ctl00_MainContent_btnSubmit').click();" />

My problem is, for reasons unbeknownst to me, the code behind isn't firing when this hidden button is clicked. If I throw an alert on the hidden button, it fires just fine, telling me the button is being clicked. The event is registered in the button's Events list, and I do have the event listener wired up:

 Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

I have commented out everything to do with creating and downloading the TDF file, instead setting a dummy error message to see if the page reacts, it does not.

Why is my code behind not running?

1

1 Answer 1

1

It appears to me that two postbacks are triggered at the same time when clicking the btnDownload button, and that they compete with each other:

  1. The postback caused by btnDownload itself
  2. The postback caused by the btnSubmit.click() call in OnClientClick

When I test your code, I get somewhat unpredictable results: the event handler btnSubmit_Click is sometimes called, sometimes not.

If you could detect in client-code that the error message is not needed anymore, you could clear it directly in OnClientClick, before the postback occurs:

<asp:Button ID="btnDownload" runat="server" Text="Download TDF" OnClientClick="clearErrorMessage();" />

function clearErrorMessage() {
    if (/* All required fields are filled */) {
        // Clear the error message
    }
}

Otherwise, you may need to respond in two steps in the event handler of btnDownload in code-behind: the first response would clear the error message, and a startup client script could be added to force a second postback. But that would not look good.

Sign up to request clarification or add additional context in comments.

1 Comment

I already feel like I've sold my soul with the two button work around. Unfortunately any javascript I put on the first button will toss an error because the master page's "error" panel is set to visible.false, so javascripts DOM can't find it unless an error has already been displayed. Adding a startup script to force the second postback worked, and will have to do. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.