Results 1 to 17 of 17

Thread: IsPostBack issues

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    IsPostBack issues

    I have this code on my asp.net page in page load event:
    vb.net Code:
    1. If Not IsPostBack Then
    2.     recordId = Request.QueryString("rid")
    3.     myRecord = GetRecord(recordId)
    4.     If myRecord Is Nothing Then
    5.         ShowErrorMessage("Record not found!")
    6.         ' pnlMainBody.Visible = False
    7.         Exit Sub
    8.     End If
    9.     ViewState("RecordId") = recordId
    10. Else
    11.     recordId = ViewState("RecordId").ToString
    12. End If
    and this javascript code:
    javascript Code:
    1. function GotoRecord() {
    2.     var tb = document.getElementById('txtRecordNumber');
    3.     if (isNaN(tb.value) || tb.value.indexOf('.') >= 0) {
    4.         tb.focus();
    5.         alert("Enter valid Record Number.");
    6.     }
    7.     else
    8.         window.location = 'ShowRecord.aspx?rid=' + tb.value;
    9. }

    It works fine until the "pnlMainBody.Visible = False" is commented out and shows the message ""Record not found!" correctly. But if I uncomment that line, it believes that it is a postback and enters the ELSE block and errors out, when user enters some record number in the textbox and hits enter (it executes the javascript code)

    Any ideas?
    Last edited by Pradeep1210; Dec 21st, 2010 at 06:51 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Hey,

    So, let me see if I have this right...

    The server side code that you have posted above, works exactly as you expect it to, until you comment on the pnlMainBody line, and then it stops working?

    I can't think of any reason what that would be happening?!?

    Gary

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    All this is on one page only. The textbox is outside the panel.

    If I put that http://site/ShowRecord.aspx?rid=xx in the browser URL it works.

    But problem is only when I put something in the textbox and use that javascript. It thinks it is a postback if that line is uncommented, and not a postback when that line is commented out.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Very bizarre! I can't say I have ever heard of something like this happening before!

    I really can't think of anything to suggest

    Gary

  5. #5

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    Also, it works if I replace that line with:
    pnlMainBody.Style("display") = "none"

    Problem occurs only when I set it to invisible. Means I make the control unavailable in the rendered page.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Quote Originally Posted by Pradeep1210 View Post
    Also, it works if I replace that line with:
    pnlMainBody.Style("display") = "none"

    Problem occurs only when I set it to invisible. Means I make the control unavailable in the rendered page.
    Really!?!?

    Setting the style directly, rather than the visible property makes this work.

    Have you tested this on a new project, i.e. completely blank, nothing else on the page apart from the bare minimum. Is it repeatble there?

    Gary

  7. #7

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    Yes. But I can see why that works.
    Setting the style will just make it invisible to the eye, but is still in the page. It is just like setting:
    <div style="display:none">

    But when we set a control Visible=False, then that control will not be available on the rendered page. It will be missing from the rendered html altogether.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Agreed, but I still don't see why uncommenting that line would effect the way that the PostBack is handled.

    It has to be altering something in the page life cycle of the page, but I can't think what, or why.

    Gary

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: IsPostBack issues

    Can't you run a WINDIF on the source of the pages created both ways and see a difference that might uncover why?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    That is a good idea. However, I would be VERY surprised if there was a difference.

    Gary

  11. #11

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    The problem is that when that line is uncommented, it goes into the ELSE block where it gets stuck with server error: "Object reference not set to an instance of an object."
    So I don't have 2 pages to compare.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Pradeep, the suggestion was to render the page to the client with the line commented out, and then take the source of the page from the browser and save it off. Then, render the page to the client with the line uncommented, and take the source of the page and save it off. Then compare the two.

    Are there any differences?

    Gary

  13. #13

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    I'm beginning to see what is going wrong...

    There is a javascript function in the TextBox onkeypress which in turn calls that javascript function I posted in post #1.
    javascript Code:
    1. function txtRecordNumber_KeyPress(event) {
    2.         if (event.keyCode == 13) GotoRecord();
    3.     }
    What happens is with that line uncommented, for some reason or the other, the page is called twice - once without a postback and once with postback. The call without postback doesn't get stuck anywhere so was not visible at first sight. While the one with postback gets stuck there, because there is no ViewState("RecordId").
    Enter keypress in the textbox is causing this. If I call the function on some button click, it works fine. Enter keypress is causing it to be called once via window.loacation=... line and once via a postback.

    Since now I can see the problem, I'll start on a workaround to it.
    I still can't figure out though, why setting the Panel.Visible = True/False is causing this problem.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Hmm, interesting....

    Can you upload a small sample application that reproduces this error? I would be curious to have a look.

    Gary

  15. #15

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    Here is a stripped down version of the page which has just the bare essential elements.

    Unfortunately on this one setting Panel.Visible=True/False has no effect, though the event is fired twice as I mentioned in the earlier post.
    My original page (problem page) is inherited from a custom basepage which does a lot of work in between for me. And I have a lot of self made custom controls too on the page. So possibly the Panel.Visible On/Off thing has to do something with that.

    Nevertheless, the root of the problem is still same - why is there two calls to the page - one without postback and one with postback? Why pressing enter key does a postback automatically to the server?

    aspx file:
    Code:
    <&#37;@ Page Language="vb" AutoEventWireup="false" Inherits="TestArea.Test" CodeFile="Test.aspx.vb"
        EnableEventValidation="true" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test Page</title>
    </head>
    <body>
        <form method="post" runat="server">
        <div>
            <br />
            <asp:Label runat="server" ID="lblMessage"></asp:Label>
            <br />
        </div>
        <div>
            <br />
            <asp:Label Text="Goto Record:" runat="server"></asp:Label>
            <asp:TextBox ID="txtRecordNumber" runat="server" onkeypress="txtRecordNumber_KeyPress(event);"
                AutoPostBack="false"></asp:TextBox>
            <input type="button" onclick="GotoRecord()" value="Show Record" />
            <a onclick="GotoRecord()">Show Record</a>
            <br />
        </div>
        <asp:Panel ID="pnlMainBody" runat="server" BorderStyle="Groove" BorderWidth="2px"
            HorizontalAlign="Left">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        Some controls in this panel
                        <br />
                        <br />
                        <asp:Label ID="Label1" Text="Label1" runat="server"></asp:Label><br />
                        <asp:ListBox runat="server" Rows="10" Width="500px" SelectionMode="Multiple"></asp:ListBox>
                        <br />
                        <asp:Button runat="server" Text="Add New" Style="width: 100px; text-align: center" /><br />
                        <br />
                    </td>
                </tr>
            </table>
        </asp:Panel>
        </form>
    </body>
    </html>
    
    <script language="javascript" type="text/javascript">
    <!--
        function GotoRecord() {
            var tb = document.getElementById('txtRecordNumber');
            if (isNaN(tb.value) || tb.value.indexOf('.') >= 0) {
                tb.focus();
                alert("Enter valid Record Number.");
            }
            else
                window.location = 'Test.aspx?rid=' + tb.value;
        }
        function txtRecordNumber_KeyPress(event) {
            if (event.keyCode == 13) GotoRecord();
        }
    -->
    </script>
    codebehind:
    Code:
    Namespace TestArea
        Partial Class Test
            Inherits System.Web.UI.Page
    
            Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                lblMessage.Text = "<br>IsPostBack = " & IsPostBack.ToString
                lblMessage.Text &= "<br>PostBack control = " & Request.Params("__EVENTTARGET")
                lblMessage.Text &= "<br>RecordId = " & Request.QueryString("rid")
    
                If IsPostBack Then
                    'pnlMainBody.Visible = False
                End If
            End Sub
        End Class
    End Namespace
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsPostBack issues

    ok... so this is what I did and it is working now.

    1. Modified the javascript code to return true for anything except ENTER.
    Code:
    <script language="javascript" type="text/javascript">
    <!--
        function GotoRecord() {
            var tb = document.getElementById('txtRecordNumber');
            if (isNaN(tb.value) || tb.value.indexOf('.') >= 0) {
                tb.focus();
                alert("Enter valid Record Number.");
                return true;
            }
            else {
                window.location = 'Test.aspx?rid=' + tb.value;
                return false;
            }
        }
        function txtRecordNumber_KeyPress(event) {
            if (event.keyCode == 13) return GotoRecord();
        }
    -->
    </script>
    2. And then passed it on to onkeypress:
    Code:
    <asp:TextBox ID="txtRecordNumber" runat="server" 
                  onkeypress="return txtRecordNumber_KeyPress(event);" 
                  AutoPostBack="false"></asp:TextBox>
    This is working for the time being, though I'll need to test more.

    But still no idea about why it is happening in the first place. There is no default button for the form or any div/panel and AutoPostBack is also false.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: IsPostBack issues

    Hmmm, still don't know what to say about this one

    Will try and do some digging later.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width