简体   繁体   中英

Count the number of dynamically generated pages

This is probably an easy solution but I can't get my head around it.

I have pages that are dynamically generated, and I count the pages that need to be generated via a LINQ statement (eg): (THIS IS UPDATED CODE to show where the questionCount is called and set to _labelQuestionCount - this is a snippet from a larger method that dynamicaly generated all pages and set all controls in dynamic pages)

using (DataClasses1DataContext db = new DataClasses1DataContext())
{
    int questionsCount = (from q in db.vw_Custom_SelectQuestionnaireQuestions        
    select q.tbl_QuestionnaireQuestion_Description).Count();
    _labelQuestionCount = questionsCount.ToString();
}

    Label labelCount = new Label();
    labelCount.ForeColor = Color.Blue;
    labelCount.Location = new System.Drawing.Point(35, 290);
    labelCount.Size = new System.Drawing.Size(150, 30);
    labelCount.Tag = _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer_Score"];
    labelCount.Text = String.Format("Question Count: {0}", _labelQuestionCount);

Here is the global var:

   string _labelQuestionCount;

The reason for this is, as I click through the dynamically created pages, I would like to "count" them so that in my "next button" event I will know the x page is my final page and I can navigate/do some other code.

Current code for next button is: (UPdated)

        void nextButton_Click(object sender, EventArgs e)
    {  
        bool c = IsChecked(_form);

        if (c == true)
        {
            var w = Convert.ToInt32(_labelQuestionCount);
            _form.Dispose();
            w--;

            if (w == 0)
            {
                //go to another page or do something else
            }
        }
        else
        {
            MessageBox.Show("You must select at least one.");
        }
    }

As you can see I try and the the value from a global variable (eg __labelQuestionCount ) and try and count it "down" until get to 0 and do something else.

But, because my pages are dynamically generated, I never get to 0.

This is probably the wrong way to go about this, so if anyone could suggest or help it would be greatly appreciated.

Kind regards, geoNeo

you have to reassign the decreased value(w--) to global variable _labelQuestionCount inside next button click event. because if you click on next the _labelQuestionCount will be remain same for all click.

I would think about assigning to a Page some int PageID , if there is no any already. And when you recover the colelction of pages from the base, you don't count the count of them but get PageID of the latest page available now .

On button_click you ask again for the last element in the colleciton at the moment of click (cause it may be was changed, new pages were added to colleciton), revoer its PageID , and check if the page you swicthing to has the same ID, if yes, do what you need to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM