ASP.Net manual postback not working from dynamical

2019-03-04 12:33发布

I'm trying to achieve a manual postback to trigger code not attached to a control. I don't want to use AJAX PageMethods as I need to reference certain controls during the postback, and want the result to update to a gridview on the page.

I already have two other methods that work, but for some reason my third one doesn't once deployed, but does work fine on my dev machine.

Code behind (Page_load)

        switch (Request["__EVENTARGUMENT"]) {
            case "ItemReserved":
                GetAllStock();
                break;
            case "PendingItemRemoved":
                PopulatePending();
                break;
            case "StockInSubmitted":
                SubmitNewStock();    // this doesn't fire
                break;
        }

I inserted a line to write to a log file as soon as the code reached "SubmitNewStock()". The log was written to on my dev machine, but not on the webserver. Both other postbacks work as expected.

Page scripts

    function ctxMenu_Reserve() {
        PageMethods.SetItemReserved(CGRefForMenu);
        __doPostBack('', 'ItemReserved');

        HideMouseMenu();
    }

    function ctxMenuPending_Remove() {
        PageMethods.RemovePendingItem(CGRefForPendingMenu);
        __doPostBack('', 'PendingItemRemoved');
    }

    function StockINSubmit(){
        SubmitPlaceHolder = document.getElementById('<%=PlaceholderStockInSubmit.ClientID %>');
        SubmitPlaceHolder.innerHTML = "Processing...";

        __doPostBack('', 'StockInSubmitted');  //Causes postback, but relevant code is not triggered once on my live server
    }

Does anyone know why this is not working once I deploy the site to my live webserver?

I should mention that StockINSubmit() is initiated from a dynamically created client link button which is placed inside a modal popup box.

        PlaceholderStockInSubmit.Controls.Add(
            new LinkButton() {
                OnClientClick = "StockINSubmit()",
                Text = "Submit",
                ID = "lnkStockInSubmit"
            });

StockINSubmit() replaces the HTML content of the DIV containing that button. (So I click "Submit", and it changes to a "Processing" label). I cant see a reason why that would cause any issues, but thought it was worth mentioning.

1条回答
forever°为你锁心
2楼-- · 2019-03-04 13:01

I eventually figured out what was happening...

The dynamically created linkbutton was performing a postback as part of its server click (onClick) event (although I had not bound any event to it). On my dev machine, the OnClientClick events postback was beating the OnClick postback, so all the expected code was running as normal.

For some reason, once deployed to the server, the servers OnClick event was beating the OnClientClick event, so the full postback was occuring without the additional __EVENTARGUMENT information that the OnClientClick event was providing.

To get around this, i found another post on this site that explained how to cancel the default postback, but still execute client side javascript.

My server side code is now updated as follows:

    LinkButton newButton = new LinkButton() {
        OnClientClick = "StockINSubmit()",
        Text = "Submit",
        ID = "lnkStockInSubmit"
    };
    newButton.Attributes.Add("onClick", "return false;");
    PlaceholderStockInSubmit.Controls.Add(newButton);

Which is now working like a charm :)

Thanks for your help guys.

查看更多
登录 后发表回答