【问题标题】:Label does not display text or update text when a specific method is called调用特定方法时,标签不显示文本或更新文本
【发布时间】:2013-03-07 05:12:50
【问题描述】:

我正在使用一个 Visual WebPart (c#),它由一个包含 Web 表单内容的 UpdatePanel 组成。在此表单上,在 UpdatePanel 中,我有一个 asp.net 标签,它的 text 属性设置基于提交的结果。它在执行后设置为可见,并且它的前景色也会根据错误或成功进行更改。这完美地工作(文本更新/变得可见),直到我决定在按下提交按钮时调用特定方法。调用此方法时,文本属性不更新,标签不显示。

我已多次调试此过程,每次我逐步完成该过程并看到正确的消息,看到它被应用于标签的文本属性,并且完成但页面没有更新。我可以通过注释掉方法调用来重现工作功能。没有错误被抛出。程序在调试时似乎可以正常执行。

提交按钮代码摘录:

try
                {
                    CreateElementOnDestinationWebs(this.ddlSupportedElements.SelectedValue.ToString(), elementName, ref messages);
                    msg = String.Format("<br />We have successfully created your {0}.  Please review the following outcomes: <br /><ul>", this.ddlSupportedElements.SelectedItem.Text);
                    foreach (string m in messages)
                    {
                        msg += String.Format("<li>{0}</li>", m);
                    }
                    msg += "</ul>";

                    labelMessages.Text = msg;
                    labelMessages.Visible = true;
                    labelMessages.ForeColor = System.Drawing.Color.ForestGreen;                        
                }
                catch (Exception ex)
                {
                    msg = String.Format("<br />An unexpected error occurred while creating the item: {0}", ex.ToString());
                    labelMessages.Text = msg;
                    labelMessages.Visible = true;
                    labelMessages.ForeColor = System.Drawing.Color.Maroon;
                }

CreateElementOnDestinationWebs 来源:

try
                    {
                        Guid listId = web.Lists.Add(name, "Student work can be submitted here.", SPListTemplateType.DocumentLibrary);
                        web.Update();

                        SPList newList = web.Lists[listId];
                        newList.OnQuickLaunch = this.checkDropBoxShowOnQuickLaunch.Checked;
                        newList.EnableModeration = true;
                        newList.Update();

                        GrantStudentGroupsContributeRights(web, ref newList, ref msgs);

                        try
                        {
                            DetectAddColumnsToDropbox(web, ref newList, ref msgs);
                        }
                        catch (Exception)
                        {
                            msgs.Add("There was an unexpected error while attempting to add the site columns to the library.");
                        }                        

                        msgs.Add(String.Format("Your item: {0}, has been successfully created on site: <a href='{1}' target='_blank'>{2}</a>", name, web.Url, web.Title));
                    }
                    catch (Exception)
                    {
                        msgs.Add(String.Format("An error occurred while creating the document library for student work on site: <a href='{0}' target='_blank'>{1}</a>", web.Url, web.Title));
                    }

如果包含“DetectAddColumnsToDropbox”的 try 块被注释掉,则标签 (labelMessages) 会正确显示。如果没有,则标签不会出现/更新。有问题的方法不会更新表单上的任何控件或项。

【问题讨论】:

  • 您能否展示您在问题中引用的代码?当您进行 PostBack 时,这个特殊问题也会发生吗?请记住,UpdatePanel 执行 Partial PostBacks
  • 按钮是否在更新面板中?对我来说,回发按钮似乎不会触发更新事件。您甚至可以显式调用面板的更新方法。你有嵌套的更新面板吗?
  • 是的。该按钮位于更新面板中。它正在完成工作。所有操作成功结束,但标签不显示文本。 UpdateProgress 代码每次都会触发。

标签: c# asp.net sharepoint-2010 web-parts


【解决方案1】:

尝试省略内部 try/catch 或将异常抛回调用方方法。

【讨论】:

  • 我注释掉了内部的 try 块,只是离开了方法调用。不幸的是,这会产生相同的结果。
【解决方案2】:

感谢大家抽出宝贵时间发表评论或尝试回答此问题。继续搜索后发现了一个相关问题:https://sharepoint.stackexchange.com/questions/5794/trying-to-use-an-spweb-object-that-has-been-closed-or-disposed-and-is-no-longer

方法:DetectAddColumnsToDropbox 为给定网站所在的网站集的 rootweb 声明了一个变量。然后它释放了 rootweb 对象。事实证明,SharePoint 不关心这一点,并且引发了 JavaScript 错误。我取出了 dispose(使用块)代码,并且一切都使用 labelMessages 正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多