0

I have a file and form called:

File: output.aspx
<form action="output.aspx?d=png" method="post" id="download-chart-form">
    <input type="hidden" id="download-chart-dataUrl" name="dataUrl" />
</form>

Then in that file I have Javascript/jQuery like so:

File output.aspx
// also have this but still not working
 $(document).ready(function ()
{
    $(function ()
    {
        var oldPostBack = __doPostBack;
        __doPostBack = function ()
        {
            $("form").triggerHandler("submit");
            oldPostBack.apply(this, arguments);
        };
    });

    ...

            var img = new Image();

            img.onload = function ()
            {
                ctxt.drawImage(this, 0, 0);
                domURL.revokeObjectURL(url);
                var dataUrl = d3Canvas.toDataURL();
                $('#download-chart-dataUrl').val(dataUrl);
                var downloadExcelForm = $('#download-chart-form');
                downloadExcelForm.submit();
            };

            img.src = url;

I get the javascript with alerts to tell that the dataUrl and all the objects are created but can't get the form sent back to output.aspx.cs.

The code behind is:

public partial class output : PageBase
{
    protected override void Page_Load(object sender, EventArgs e) 
    {
        base.Page_Load(sender, e);

        if (!IsPostBack)
        {
            string png = Request["d"] ?? string.Empty;

            if (string.IsNullOrEmpty(d))
            {
             ...
cdub
  • 24,555
  • 57
  • 174
  • 303
  • AFAIK to trigger form submit to code behind using jQuery it is necessary to modify `__doPostBack` with submit `triggerHandler` so that `$('form').submit(...)` may initiate postback event. – Tetsuya Yamamoto Aug 09 '18 at 02:25
  • I've forgotten how to do that – cdub Aug 09 '18 at 02:30
  • Try approach described in [this post](https://stackoverflow.com/a/21490428). It called "`__doPostBack` hijacking" by applying jQuery handler to standard postback function provided by ASP.NET. – Tetsuya Yamamoto Aug 09 '18 at 02:36
  • Something is still amiss. I updated my code – cdub Aug 09 '18 at 02:59
  • Possibly you should consider using `RegisterOnSubmitStatement` in addition to "postback hijacking", as provided [here](https://stackoverflow.com/questions/1230573/how-to-capture-submit-event-using-jquery-in-an-asp-net-application). Since postback in webforms has different cycles compared to other stateful frameworks, it requires some tweaks to get `$('form').submit()` work. – Tetsuya Yamamoto Aug 09 '18 at 03:22

0 Answers0