简体   繁体   English

计算动态生成的页面数

[英]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) 我有动态生成的页面,并计算了需要通过LINQ语句生成的页面(例如):(此更新代码显示了在哪里调用questionCount并将其设置为_labelQuestionCount -这是来自较大代码段的摘录动态生成所有页面并在动态页面中设置所有控件的方法)

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. 原因是,当我单击动态创建的页面时,我想对其进行“计数”,以便在我的“下一个按钮”事件中,我将知道x页面是我的最终页面,并且我可以导航/执行其他操作码。

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. 如您所见,我尝试使用全局变量(例如__labelQuestionCount )中的值,并尝试对其进行“递减”计数直至达到0并执行其他操作。

But, because my pages are dynamically generated, I never get to 0. 但是,因为我的页面是动态生成的,所以我永远都不会达到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 亲切的问候,geoNeo

you have to reassign the decreased value(w--) to global variable _labelQuestionCount inside next button click event. 您必须在下一个按钮单击事件内将减小的值(w--)重新分配给全局变量_labelQuestionCount。 because if you click on next the _labelQuestionCount will be remain same for all click. 因为如果您单击下一步,则_labelQuestionCount对于所有单击将保持不变。

I would think about assigning to a Page some int PageID , if there is no any already. 我会考虑为Page分配一些int PageID ,如果还没有的话。 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 . 而且,当您从基础中恢复页面的集合时,您无需计算它们的数量,而是获得现在可用的最新页面的PageID

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. button_click在单击时再次询问大学中的最后一个元素(因为它可能已更改,新页面已添加到大学中),重新命名其PageID ,并检查您要摆动的页面是否具有相同的ID,如果是,请执行所需的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript计算动态生成的HTML表中单元格中文本框的数量? - How to count number of the textbox within a cell in a dynamically generated HTML Table using a JavaScript? 计算打印页数的有效方法? - Efficient way to count number of pages to print? 如何获得radgrid上的页数计数 - How to get the number of pages count on radgrid 带有从javascript动态生成html的页面的HttpWebRequest吗? - HttpWebRequest with pages that have dynamically generated html from javascript? 动态生成的 Blazor 页面仅在页面导航之间部分更新 - Dynamically generated Blazor pages are only partially updated between page navigation 如何在ASP.NET MVC中统计动态生成的控件 - How To Make Count Of Dynamically Generated Controls In ASP.NET MVC 如何使用iTextSharp动态生成的PDF在某些页面上做不同的事情? - How to do something different on some pages in a dynamically generated PDF generated with iTextSharp? 如何计算在C#中使用数组随机生成的每个数字 - How to count each number that is generated randomly using array in C# 在WinForm C#中使用ZedGraph动态生成选项卡页数 - Make dynamically number of tab pages with ZedGraph in WinForm C# 如何使用C#计算打印预览Excel中的打印页数? - How To Count The Number Of Printed Pages In Print Preview Excel with C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM