JosephStyons
JosephStyons

Reputation: 58785

Javascript OnPostback

I have a user control A that is sitting on a page alongside another user control B.

Control A has it's own functionality, which includes some controls that cause postbacks (drop downs, checkboxes, etc).

I control B. I don't control A.

B needs to hide a button that exists in A.

In B, I have markup and script like so:

<body onload="OnPageLoad();">
...

  function OnPageLoad() {
    $("input[name$='btnInPartA']").hide();
...

This successfully hides the button in A when the page is loaded.

Whenever a postback is fired from A, then the button reappears.

How can I keep that button hidden? I'd like to fire my javascript code every time a postback occurs, but I'm open to other ideas.

Here is the markup for part B:

<body onload="OnPageLoad();">
  <div id="divDonationExtensionFields">
    <div id="divSubmitButton">
      <input type="button" id="btnCZDonate" value="Donate Now" onclick="btnCZDonate_OnClick();" />
    </div>
  </div>
</body>

<script type="text/javascript">
  function OnPageLoad() {
    alert("OnPageLoad fired!");
    $("input[name$='btnNext']").hide();
  }

  function btnCZDonate_OnClick() {
    //do whatever part A was going to do
    $("input[name$='btnNext']").click();
    //do some custom stuff after that
  }
</script>

Upvotes: 2

Views: 286

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039428

Try using the pageLoad special function instead of subscribing to the onload handler. The difference is that pageLoad will be invoked on every postback no matter if it is partial or not. It seems that there's some partial postback being done which is re-showing the button you have hidden and your onload function won't be invoked in this case:

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function pageLoad() {
            alert("OnPageLoad fired!");
            $("input[name$='btnNext']").hide();
        }
    </script>
</head>
<body>
    ...
</body>

You can read more about pageLoad and the difference with $(document).ready (or onload) in this article.

Upvotes: 2

Related Questions