ASP.Net manual postback not working from dynamically created linkbutton

前端 未结 1 1220
旧时难觅i
旧时难觅i 2021-01-27 18:51

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 post

1条回答
  •  孤街浪徒
    2021-01-27 19:12

    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.

    0 讨论(0)
提交回复
热议问题