Dynamic control click event not firing properly

Posted by Wil on Stack Overflow See other posts from Stack Overflow or by Wil
Published on 2010-06-07T06:41:22Z Indexed on 2010/06/07 8:22 UTC
Read the original article Hit count: 187

Filed under:
|

I'm creating a next/previous function for my repeater using pageddatasource. I added the link button control dynamically in my oninit using the following code.

LinkButton lnkNext = new LinkButton();
lnkNext.Text = "Next";
lnkNext.Click += new EventHandler(NextPage);

if (currentPage != objPagedDataSource.PageCount)
{
    pnlMain.Controls.Add(lnkNext);
}

So in my initial page_load, the next link comes up fine. There are 5 pages in my objPagedDataSource. currentPage variable is 1.

The "NextPage" event handler looks like this

public void NextPage(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Cookies["PageNum"] == null)
    {
        HttpCookie cookie = new HttpCookie("PageNum");
        cookie.Value = "1";
    }
    else
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["PageNum"];
        cookie.Value = (Convert.ToInt32(cookie.Value) + 1).ToString();
    }

    this.BindRepeater();
}

So I am incrementing the cookie I am using to track the page number and then rebinding the repeater.

Here is the main issue. The first time I click Next, it works, it goes to Page 2 without any problems. When on Page 2, I click Next, it goes back to Page 1. Seems like the Next event is not wiring up properly. Not sure why, any ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET