문제

I have a 'shop' page, where all the items in the shop is listed. I have a sortingfunction, to decide how many results pr. page I want to see.

I also have a pager. The pager is built with two buttons (a Previous- and Next-pagebutton), and a textbox which shows the pagenumber. The pager is on both the top, and bottom of the page.

When I change the text in the textbox, the following code fires:

protected void tbPageNumberTop_TextChanged(object sender, EventArgs e)
    {
        tbPageNumberBottom.Text = tbPageNumberTop.Text;
        updpanMain.Update();
    }

This causes a pageload, where I get the number from the textbox, and parse it to an integer, which I can use to dynamically create an SQL request. This works just perfectly.

Here's my problem. When I click the "Next" button or "Previous" button, the text in the textbox changes, but when I get the number from the textbox, I get the value of the textbox as it was BEFORE the button was clicked. How do I obtain the correct pagenumber?

Here's my Click-code:

protected void btnNextPage_Click(object sender, EventArgs e)
    {
        int PageNumber = Convert.ToInt32(tbPageNumberTop.Text);
        PageNumber += 1;
        tbPageNumberTop.Text = PageNumber.ToString();
        tbPageNumberBottom.Text = PageNumber.ToString();
        updpanMain.Update();
    }
    protected void btnPrevPage_Click(object sender, EventArgs e)
    {
        int PageNumber = Convert.ToInt32(tbPageNumberTop.Text);
        PageNumber -= 1;
        tbPageNumberTop.Text = PageNumber.ToString();
        tbPageNumberBottom.Text = PageNumber.ToString();
        updpanMain.Update();
    }

And here's what happens on page_load

if (!this.IsPostBack)
        {
            intStartPosition = 0;
            intPageNumber = 1;
            tbPageNumberBottom.Text = intPageNumber.ToString();
            tbPageNumberTop.Text = intPageNumber.ToString();
        }

        if (tbPageNumberTop.Text != "1" || tbPageNumberBottom.Text != "1")
        {
            intPageNumber = Convert.ToInt32(tbPageNumberTop.Text);
        }
        else
        {
            intPageNumber = 1;
        }


        intStartPosition = (intPageNumber * intItemsPrPage) - intItemsPrPage;
        strResultsPrPage = intItemsPrPage.ToString();

intStartPosition represents the first LIMIT integer in my SQL string, and strResultsPrPage represents the second integer, i.e. LIMIT 100, 500 is showing results from 500 to 600. (Page 5, with 100 results per page)

도움이 되었습니까?

해결책

Your Page_Load event is going to fire before your button's Click event. It looks like you're setting the parameters used in your SQL call (intStartPosition and strResultsPrPage) in the Page_Load event based on the value retrieved from the textbox (tbPageNumberTop.Text) before the Click event has had a chance to update that value.

You'll need to find a way to delay that SQL call until after your Click events have had a chance to update the current page value.

You may want to have a look at the Page Life Cycle for clarification:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

다른 팁

Problem solved.

What I did was the following:

In the page_load code, I wrote the following:

protected void Page_Load(object sender, EventArgs e)
    {
        Page.LoadComplete += new EventHandler(Page_LoadComplete);
    }

And then I pasted the old page_load codeblock inside the new eventhandler, at it works like a charm.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top