5

I have a table with all the objects I have in my db. I load them in my Page_Load function. I have a text field and a button that when clicking the button, I want the handler of that click to put a new object with the name written in the text field in the db.

Now, I want that what happens after the click is that the page loads again with the new item in the table. The problem is that the button event handler is run after the Page_Load function.

A solution to this would be to use IsPostBack in the Page_Load or use the pre load function. A problem is that if I would have 3 different buttons, I would have to differ between them there instead of having 3 different convenient functions.

Any solutions that don't have this problem?

Code:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userId"] == null)
                Response.Redirect("Login.aspx");

            // LOAD DATA FROM DB
        }

        protected void CreateObject(object sender, EventArgs e)
        {
            // SAVE THE NEW OBJECT
        }
Vadiklk
  • 3,696
  • 5
  • 28
  • 44
  • 3
    you can't have click event before page_load, but I don't see how this problem is not solved by using IsPostBack ? – Antonio Bakula Apr 30 '12 at 13:26
  • It is, but what if I would have 12 button calls? I will then have, instead of 12 functions, 12 if clauses. – Vadiklk Apr 30 '12 at 14:38
  • really don't understand, you will have only one if: IsPostBack, if true don't excecute code in OnLoad whatever that was, and button click events will do it's business. Button click events will still happen – Antonio Bakula Apr 30 '12 at 14:41
  • 1
    And how would I once again fill the table with data from the db if I won't execute the function? – Vadiklk Apr 30 '12 at 14:42
  • well I don't see all your code, but maybe this is not needed if you fill controls like gridview becouse they will maintain this data in viewstate. Or you can call fill in button click events. No offense, but you should familiarize your self with common patterns in ASP.NET web forms : http://www.asp.net/web-forms. Or better yet try ASP.NET MVC – Antonio Bakula Apr 30 '12 at 14:52

5 Answers5

9

Maybe you should try loading your data during PreRender instead of Load

    protected void Page_Load(object sender, EventArgs e)
    {
        this.PreRender += Page_PreRender
        if (Session["userId"] == null)
            Response.Redirect("Login.aspx");
    }

    protected bool reloadNeeded {get; set;}

    protected void CreateObject(object sender, EventArgs e)
    {
        // SAVE THE NEW OBJECT
        reloadNeeded = true;
    }


    protected void Page_PreRender(object sender, EventArgs e)
    {
        if(reloadNeeded || !IsPostBack)
        // LOAD DATA FROM DB
    }
jbl
  • 15,179
  • 3
  • 34
  • 101
  • I like your solution, but it doesn't seem to work. It enters the function, but it doesn't show the data in the table, not even for the first time. Maybe it is because I call the Page_PreRender, LoadData? – Vadiklk Apr 30 '12 at 15:19
  • @Vadiklk : Difficult to answer without the whole code. I assumed "LOAD DATA FROM DB" also includes the databinding part (setting a control DataSource and calling DataBind) If you still do part of the binding during Load. It won't work. Also, prerender is not triggered on not visible controls. – jbl Apr 30 '12 at 15:27
  • I do the binding in my LoadData function (your Page_PreRender function) – Vadiklk Apr 30 '12 at 15:37
2

You can check the event target and do what you need then:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
         string eventTarget = Page.Request.Params["__EVENTTARGET"];
         if(whatever)
         {
             //do your logic here
         }
    }
}

Get control name in Page_Load event which make the post back

rick schott
  • 21,012
  • 5
  • 52
  • 81
0

Use the Page_PreRenderComplete event to retrieve your table. That way your page will always have the most recent data available after all user events have fired.

Charles Lambert
  • 5,042
  • 26
  • 47
0

Why don't you move what you have in the click event into a new method. Then call that method as the first line in your page load?

Arcturus
  • 2,902
  • 2
  • 19
  • 11
0

An old question but I faced the same problem in my C#/ASP.NET Website with master/content pages: a click on a link on the master page should change a query parameter for a gridview on the content page. As you stated the button click event is handled after Page_Load. But it is handled before Page_LoadComplete (see the information about ASP.NET Page Life Cycle), so you can change the page content there.

In my case I solved the problem by setting a session variable in the click event in the master page, getting this variable in the Page_LoadComplete event in the content page and doing databind based on that.

Master page:

<asp:LinkButton ID="Btn1" runat="server" OnCommand="LinkCommand" CommandArgument="1" >Action 1</asp:LinkButton>
<asp:LinkButton ID="Btn2" runat="server" OnCommand="LinkCommand" CommandArgument="2" >Action 2</asp:LinkButton>

protected void LinkCommand(object sender, CommandEventArgs e)
{
  HttpContext.Current.Session["BindInfo", e.CommandArgument.ToString());
}

Content page:

protected void Page_LoadComplete(object sender, EventArgs e)
{
  string BindInfo = HttpContext.Current.Session["BindInfo"].ToString();
  YourBindDataFunction(BindInfo);
}
Joep
  • 146
  • 2
  • 6