Label在调用特定方法时不显示文本或更新文本

本文关键字:文本 显示 更新 方法 调用 Label | 更新日期: 2023-09-27 18:04:37

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

我已经调试了这个过程几次,每次我通过这个过程并看到正确的消息,看到它被应用到标签的文本属性,并完成,但页面没有更新。我可以通过注释掉方法调用来重现工作功能。没有抛出错误。在调试过程中,程序执行正常。

提交按钮代码摘录:

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)将正确显示。如果没有,标签就不会出现/更新。该方法不更新表单上的任何控件或项。

Label在调用特定方法时不显示文本或更新文本

尝试省略内部的Try/catch或将异常扔回调用方方法。

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

方法DetectAddColumnsToDropbox声明了给定web所在的站点集合的rootweb的变量。然后它处理了rootweb对象。事实证明,SharePoint并不关心这个问题,并且抛出了一个JavaScript错误。我取出了dispose(使用block)代码,所有功能都可以正常使用labelMessages。