Search code examples
c#asp.netfunctionispostback

ASP .net PostBack and Function


I'm working on a web page, I need that when it loads first time it would get data from api's.

Then I want to go from page to page with the same data.

I used IsPostBack

Page_Load(object sender, EventArgs e)
{
        if (!Page.IsPostBack)
        {
            //First Time Load Data
            leadsList = new List<Lead>();
            var client = TVPLeads.Session.GetWebClient(Session);
            string PostId = PostId = Request.QueryString["postId"];
            PostId = "1";
            try
            {
                leadsList = API.GetPostLeads(client, PostId);
            }
            catch (Exception) { }
            page = (page == 0) ? 0 : page - 1;
            DisplayLeadsPage(leadsList, page, "");

        }

}

private void pageChoosen(object sender, EventArgs e)
{
    int page = int.Parse(((Button)sender).CommandArgument);
    DisplayLeadsPage(leadsList, page-1, "");
}

DisplayPagination(){
.
.
 Button prev = new Button{
                    ID = "page_"+i,
                    CssClass = "Pagination",
                    Text = "i",
                    CommandArgument = ""+i,
                    OnClientClick = "pageChoosen"
                };
                prev.Click += pageChoosen;

                divPagination.Controls.Add(prev);
.
.
.
}

I clicking on a button , got to Page_Load function the postBack is true as expected , but the function is not firing(checked with debugger).

  • if I remove the IsPostBack and it would make all over again , then the button function is firing.

What's the problem with that? How to use it right ?


Solution

  • The first time you request your page, your page is not posted back. The rendering engine of asp.net creates the page and sends it back to the client.

    When you click a button then this click we trigger a postback and a handler that is defined the Page class will execute some code. Then the page will be build and when on Page_Load comes into the scene, the code that is in the if(!Page.IsPostBack) will not be executed, because the IsPostBack property of the Page class is true.

    There are two key concepts to conceive there. The first is about the Page lifecycle, the events that the page goes through in each request.

    The second is that the Page is not posted back only the first time the client requests it (or the times that she does a full refresh of the page, clicking F5 for instance in Chrome).

    Update

    Respond to button click

    If you haven't defined a server side button, you should define one. How?

    In you markup, just add the following line of code:

    <asp:Button id="buttonId" Text="Submit" runat="server" />
    

    Then in the designer double click on the button. As you will notice, you will be navigated to the code behind class and a method would have been created, which would be the click handler. Then inside the body of this method, you could write the commands that should be executed, when the user clicks on your button. If you now go back to your markup, you will notice the another attribute would have been added to the Button with name OnClick.

    Update

    Dynamically built buttons need to be created again when the page is posting back. Otherwise they won't work.