为什么在preRender被称为一遍又一遍

为什么在preRender被称为一遍又一遍

本文介绍了为什么在preRender被称为一遍又一遍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的SharePoint 2010的WebPart,我有这样的code:

In my Sharepoint 2010 WebPart, I have this code:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    if (this.dpsvisWebPart != null && this.dpsvisWebPart.CustomTitleProp != null)
    {
        lbl_Title.Text = String.Format("<h1>{0}</h1>", this.dpsvisWebPart.CustomTitleProp.ToString());

        if (this.dpsvisWebPart.CheckboxGenSection1)
        {
            GenerateSection1();
        }
        if (this.dpsvisWebPart.CheckboxGenSection2)
        {
            GenerateSection2();
        }
        if (this.dpsvisWebPart.CheckboxGenSection3)
        {
            GenerateSection3();
        }
        if (this.dpsvisWebPart.CheckboxGenSection4)
        {
            GenerateSection4();
        }
        if (this.dpsvisWebPart.CheckboxGenSection5)
        {
            GenerateSection5();
        }
        if (this.dpsvisWebPart.CheckboxGenSection6)
        {
            GenerateSection6();
        }
        if (this.dpsvisWebPart.CheckboxGenSection7)
        {
            GenerateSection7();
        }

        if (AnyCheckboxSelected())
        {
            // Create Save button
            this.Controls.Add(new LiteralControl("<br />"));
            Button btnSave = new Button();
            btnSave.Text = "Save";
            btnSave.Click += new EventHandler(btnSave_Click);
            this.Controls.Add(btnSave);

            AddVerticalSpace();
        }
    }
}

在测试我的WebPart(点击按钮,从而 - presumably - 执行btnSave_Click处理程序中,我发现,什么也没有被保存

On testing my WebPart (clicking the button, thus - presumably - executing the btnSave_Click handler, I found that nothing was being saved.

通过code步进,我看到在preRender正在达到 - 事实上,一遍又一遍。

Stepping through the code, I see that OnPreRender is being reached - in fact, over and over again.

所以,我说这WebPart类:

So, I added this to the WebPart class:

private bool PreRenderAlreadyRun = false;

...然后改变了preRender()的开头这样:

...and then changed the beginning of OnPreRender() to this:

protected override void OnPreRender(EventArgs e)
{
    if (PreRenderAlreadyRun) return;
    PreRenderAlreadyRun = true;
    base.OnPreRender(e);
    . . .

...但是preRenderAlreadyRun总是false在preRender输入时,这恰好一遍又一遍。因为页面是一个无限循环的其他断点没有达到(按钮点击等)presumably。

...but PreRenderAlreadyRun is always false when OnPreRender is entered, which happens over and over. Other breakpoints are not reached (the button click, etc.) presumably because the page is in an endless loop.

我怎样才能在preRender()只运行一次?或者应该这样code在的Page_Load(),而不是在preRender(),还是......?

How can I get OnPreRender() to run only once? Or should this code be in Page_Load() instead of OnPreRender(), or...???

推荐答案

解决方案是移动code,它是在preRender()来的Page_Load()

The solution was to move the code that was in OnPreRender() to Page_Load()

显然,就像在堆场,渲染发生很多次,而页面只装载一次。这是我的理论,我坚持'它(就目前而言)。

Apparently, just like at the stockyard, rendering happens many times, while the page is only loaded once. That's my theory, and I'm stickin' to it (for now, anyway).

这篇关于为什么在preRender被称为一遍又一遍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:11