Page view counter

Javascript: detect Page.IsValid and IsPostBack

Last post 03-04-2009 12:20 PM by CptCoathanger. 13 replies.

Sort Posts:

  • Javascript: detect Page.IsValid and IsPostBack

    08-31-2007, 10:37 AM
    • Loading...
    • jon_angel
    • Joined on 02-03-2006, 5:59 PM
    • Posts 12
    • Points 35

    In a client-side function that initializes a 3rd-party control, I want to detect whether the page is being loaded for the first time or as the result of a post back.  If it is a post back, I want to know if on posting the original page was valid.

    I tried using Page_ClientValidate() within the function, but that validates the page, and fires the RequiredFieldValidator, which isn't what I want to do at all!

    many thanks,  

     

    Jon Angel
    US PHS / HHS
  • Re: Javascript: detect Page.IsValid and IsPostBack

    08-31-2007, 11:45 AM
    • Loading...
    • mattmeckes
    • Joined on 08-31-2007, 2:52 PM
    • Posts 3
    • Points 16

    If you are doing this client side I dont think you can check if it is posted back. You could create a cookie when the page is first loaded. You can store the value of the global client side variable Page_IsValid in the cookie. If the page is posted back the cookie will already exist and you can check if the original was valid.

  • Re: Javascript: detect Page.IsValid and IsPostBack

    08-31-2007, 12:07 PM
    • Loading...
    • jon_angel
    • Joined on 02-03-2006, 5:59 PM
    • Posts 12
    • Points 35

    Cookies probably aren't an option, according to policy.  What would work is detecting that the RequiredFieldValidator has fired and is displaying its error message.  But in debug mode I couldn't find a property of the validator nor the RadioButtonList that it validates that tells me this.

    Jon Angel
    US PHS / HHS
  • Re: Javascript: detect Page.IsValid and IsPostBack

    08-31-2007, 12:51 PM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    It's easy enough to detect whether a PostBack has occurred client-side:

    In the aspx file:

    <form id="Form1" method="post" runat="server">
     <p>
      <asp:button id="postBackButton" runat="server" text="PostBack"></asp:button>
     </p>
     <p>
      <input type="button" value="Check for PostBack" onclick="JavaScript: alert('IsPostBack: ' + isPostBack());">
     </p>

     <input type="hidden" id="clientSideIsPostBack" name="clientSideIsPostBack" runat="server">

     <script type="text/javascript">
     function isPostBack()
     {
      if ( !document.getElementById('clientSideIsPostBack') )
       return false;

      if ( document.getElementById('clientSideIsPostBack').value == 'Y' )
       return true;
      else
       return false;
     }
     </script>
    </form>

    In the aspx.cs file:

    private void Page_Load(object sender, System.EventArgs e)
    {
     if ( this.IsPostBack )
      this.clientSideIsPostBack.Value = "Y";
     else
      this.clientSideIsPostBack.Value = "N";
    }

    NC...

  • Re: Javascript: detect Page.IsValid and IsPostBack

    09-04-2007, 6:08 PM
    • Loading...
    • jon_angel
    • Joined on 02-03-2006, 5:59 PM
    • Posts 12
    • Points 35

    Many thanks, I'm almost there, but I'm getting a weird error 'Restricted_COER_Attachments' does not contain a definition for 'clientSideIsPostBack' 

    Intellisense sure finds a definition, but Compilation doesn't.  If I add an explicit definition in the aspx.cs, Intellisense tells me the HtmlHiddenInput object is defined twice.  Dragging an Input (Hidden) from the HTML section of the toolbox makes no difference.

    However if I Build Page it works fine (?!)

     

    Jon Angel
    US PHS / HHS
  • Re: Javascript: detect Page.IsValid and IsPostBack

    09-05-2007, 7:11 AM
    • Loading...
    • jsearles
    • Joined on 08-02-2006, 6:54 AM
    • Posts 89
    • Points 336
    What i have always done to deal with this is declare a global variable in javascript and then set it after the initial page load. For example: var isPostBack = false; function pageLoad() { if (!isPostBack) { // Do stuff isPostBack = true; } } It's always worked for me.
  • Re: Javascript: detect Page.IsValid and IsPostBack

    09-05-2007, 10:51 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    Never heard of a 'Restricted_COER_Attachments'. Maybe it is coming from something that your 3rd party app is inserting?

    The idea that jsearles presented should also work however:

    In the aspx file:

    <script type="text/javascript">
    function isPostBack()
    {
     var serverSidePostBack = '<%= clientSideIsPostBack %>';

     return (serverSidePostBack == 'Y') ? true : false;
    }
    </script>

    In the aspx.cs file:

    // Declare outside of any method:
    public string clientSideIsPostBack = "N";

    private void Page_Load(object sender, System.EventArgs e)
    {
     if ( this.IsPostBack )
      this.clientSideIsPostBack = "Y";
     else
      this.clientSideIsPostBack = "N";
    }

    NC...

  • Re: Javascript: detect Page.IsValid and IsPostBack

    10-22-2008, 2:36 PM
    • Loading...
    • RobSil
    • Joined on 07-03-2008, 4:20 PM
    • Posts 136
    • Points 68

     To extend all this a little bit further...

     

    <script type="text/javascript">
            function bodyOnLoad() {
            alert("Is Postback: <%Response.Write(Page.IsPostBack);%>");
            
            }
      </script>

     

    <body onload="bodyOnLoad()">... </body>

     

    Just use the postback variable already in the page.  

     

    HTH,

    Robin

     

  • Re: Javascript: detect Page.IsValid and IsPostBack

    10-22-2008, 3:03 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    Why reply on a post that is over a year old?

    NC...

     

  • Re: Javascript: detect Page.IsValid and IsPostBack

    10-22-2008, 3:05 PM
    • Loading...
    • RobSil
    • Joined on 07-03-2008, 4:20 PM
    • Posts 136
    • Points 68

     

    NC01:

    Why reply on a post that is over a year old?

    NC...

    Why not?  I came across the post researching a problem, so others might as well.  

  • Re: Javascript: detect Page.IsValid and IsPostBack

    10-22-2008, 3:08 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    Because the post has been resolved and you really did not contribute much if anything different than what was already posted.

    NC...

     

  • Re: Javascript: detect Page.IsValid and IsPostBack

    10-22-2008, 3:22 PM
    • Loading...
    • RobSil
    • Joined on 07-03-2008, 4:20 PM
    • Posts 136
    • Points 68

    NC01:

    Because the post has been resolved and you really did not contribute much if anything different than what was already posted.

    NC...

     

     

    No contribution?   I shortened your original post into not requiring ANY server-side coding, you don't consider that a contribution?  This means no custom variables (a string at that?) and no need to get/set them in the Page_Load EVERY time you implement this approach.

    Since you seem willing to debate minute details about an implementation... why would you ever track the exact same variable twice in your code-behind if you don't need to?  Not only that, your tracking it as a string rather than a bool, which is much more in-efficient.  Finally, your assuming that it will be set in every Page_Load which means the user would need to repeat unnecessary code in his Page_Load's.  Also worth pointing out that in the first response you made in this thread, you used a hidden element when it is not needed.

     Back to your previous post, this thread will be read for some time I am sure (clearly if I found it year(s) after it was originally discussed), so why not attempt to provide the best/easiest solution possible? 

  • Re: Javascript: detect Page.IsValid and IsPostBack

    01-27-2009, 1:24 PM
    • Loading...
    • d4446
    • Joined on 01-27-2009, 6:21 PM
    • Posts 1
    • Points 2

    It helped me and I believe you contributed more than all previous posts. 

  • Re: Javascript: detect Page.IsValid and IsPostBack

    03-04-2009, 12:20 PM
    • Loading...
    • CptCoathanger
    • Joined on 05-16-2007, 4:48 AM
    • Posts 6
    • Points 6

     Nice one, dude.  Should have thought of it myself really!  Don't pay any attention to the miserable chap bemoaning you resurrecting a thread, he must have a little sand in his vagina. Wink

Page 1 of 1 (14 items)