Puppy
Puppy

Reputation: 146910

Button not causing postback

I've got a Button, with the corresponding aspx being this:

<asp:Button runat="server" ID="checkall" OnClientClick="setAllWeeks(true);" 
    AutoPostBack="true" Text="Check All" />

This is because the logic I want to happen for this particular button on the server side is validation that occurs every time in the Page_Load event, and additionally, I want a Javascript function to be called first. However, when I click the button, it doesn't seem to cause a postback the first time, only the second time that it's posted.

How can I guarantee that the client-side function is executed first, and then a postback is generated?

Upvotes: 0

Views: 9752

Answers (2)

Ahmy
Ahmy

Reputation: 5480

Do the following code:

<asp:Button runat="server" ID="checkall" OnClientClick="return setAllWeeks(true);" 
    AutoPostBack="true" Text="Check All" />

And inside the setAllWeeks() function do the following :

function setAllWeeks(val) {

    //If everything all right
    If(True)
    {
      //Do something
      return true;   
    }
    else
      return false;
}

If the function behaviors is all right return true else return false.

Note: All paths of the function should return a value(Boolean value).

Upvotes: 1

TweeZz
TweeZz

Reputation: 4909

This happened to me a few times. The cause was that my function specified in OnClientClick returned 'false'.

Hoe does that 'setAllWeeks' look like? Maybe it's returning false?

Upvotes: 0

Related Questions