locked
Page.IsPostBack RRS feed

  • Question

  • User-390313747 posted

    Hi,

    I'm trying to understand what Page.IsPostBack is and when it is fired. To do a test I set up a simple check in the Page_Load method of an aspx page with the following code:

    if (!Page.IsPostBack)
    {
        Response.Write("Check");
    }

     

    When I loaded the page from Visual Studio the message showed. When I refreshed the page the message was still there and when I changed a list item on a drop down list that had AutoPostBack set to true it was still showing. So when would this message not appear?

    Friday, February 25, 2011 11:52 AM

Answers

  • User-1360095595 posted

    I believe the first claim when refreshing the page.  That's like loading the page from scratch.

    But the second one I don't understand.  It shouldn't.  Unless you're using an updatepanel.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 25, 2011 11:56 AM
  • User1943143334 posted

    Hi,

    For any event on the page, postback occurs. In your case, post back should occur if you change the  drop downlist item and it should not show the data. Can you show the dropdownlist code?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 25, 2011 1:33 PM
  • User-1373473781 posted

    Page.IsPostBack event normally fire when you asp control fire any server side event

    • buttonClick
    • SelectedIndexChanged
    • textChanged

    when you set the property

    for eg:

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

    when your selected index change it will call server side method

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
            
    }

    I hope this will help you to understand the post back scenario

    Go through this LINK http://msdn.microsoft.com/en-us/library/ms178472.aspx

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 25, 2011 1:38 PM

All replies

  • User-1360095595 posted

    I believe the first claim when refreshing the page.  That's like loading the page from scratch.

    But the second one I don't understand.  It shouldn't.  Unless you're using an updatepanel.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 25, 2011 11:56 AM
  • User-390313747 posted

    No nothing out of the ordinary is being used as I said all I did was refresh the page and in reference to the drop down list it is just held in a div nothing else

    Friday, February 25, 2011 12:16 PM
  • User1943143334 posted

    Hi,

    For any event on the page, postback occurs. In your case, post back should occur if you change the  drop downlist item and it should not show the data. Can you show the dropdownlist code?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 25, 2011 1:33 PM
  • User-1373473781 posted

    Page.IsPostBack event normally fire when you asp control fire any server side event

    • buttonClick
    • SelectedIndexChanged
    • textChanged

    when you set the property

    for eg:

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

    when your selected index change it will call server side method

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
            
    }

    I hope this will help you to understand the post back scenario

    Go through this LINK http://msdn.microsoft.com/en-us/library/ms178472.aspx

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 25, 2011 1:38 PM
  • User-390313747 posted

    The event that is firing is SelectedIndexChanged but as you say this should be post back and shouldn't execute the code. Also, should refreshing the page cause the code to execute? The code for the drop down list is very simple: <asp:dropdownlist onselectedindexchanged="TestList_SelectedIndexChanged" autopostback="True" runat="server" id="TestList"> <asp:listitem value="One" text="First Item"></asp:listitem> <asp:listitem value="Two" text="Second Item"></asp:listitem> </asp:dropdownlist>

    <asp:DropDownList ID="TestList" runat="server" AutoPostBack="True" 
                        onselectedindexchanged="TestList_SelectedIndexChanged">
                        <asp:ListItem Text="First Item" Value="One"></asp:ListItem>
                        <asp:ListItem Text="Second Item" Value="Two"></asp:ListItem>
                    </asp:DropDownList>

    Horrible names I know but it really is just for test purposes to see how IsPostBack works. I don't know if this would make a difference but this is held in a Master Page

    Friday, February 25, 2011 1:42 PM
  • User-1373473781 posted

    for eg:

    • Take a GridView.
    • Define a method BindGrid() in which you assign datasource to you GridView
    • Try with two scenario on you Page_Load Event

    a) if(!IsPostBack)

        BindGrid()

     

    b)BindGrid()

    You will observe the Page.IsPostBack

    I hope this will help you to under stand the scenarion better.

    Friday, February 25, 2011 1:51 PM
  • User-390313747 posted
    I had a redirect in the SelectedIndexChanged which I had forgotten all about and is the source of the confusion. So in other words it was me being silly. Thanks for the help
    Friday, February 25, 2011 1:56 PM