ASP.Net manual postback not working from dynamically created linkbutton

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x