0

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.

user1830285
  • 578
  • 8
  • 24
  • dunno but once I tried to call __doPostBack only to realize that __doPostBack is only rendered if there is a control that needs a JS postback simulator. So I added a link button to the page. Check to see if __doPostBack is in the rendered markup. – MatthewMartin Nov 20 '12 at 14:40
  • 2
    I've answered a question on how to create the __doPostBack correctly using managed code: http://stackoverflow.com/questions/10483271/need-to-call-server-side-event-using-dopostback/10483439#10483439 – CAbbott Nov 20 '12 at 14:48
  • @MatthewMartin __doPostback is indeed rendered to the page, as I have quite a few controls with postbacks associated with them. – user1830285 Nov 20 '12 at 15:03
  • @CAbbott I looked at this, but how do I get a PostBackEventReference to an event (there is no control). I get compile errors when I attempt this – user1830285 Nov 20 '12 at 15:04
  • Having a dynamically added control does make it trickier. There is a control (the LinkButton), but my guess would be that you need to use the PostBackEventReference for that dynamically added control. – CAbbott Nov 20 '12 at 15:34
  • Correct. Really the simplest method is to just post back to the Page_Load event, and perform the required action based on the event reference? Which is working until I publish the site to the server. – user1830285 Nov 20 '12 at 15:42

1 Answers1

0

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.

Community
  • 1
  • 1
user1830285
  • 578
  • 8
  • 24