【问题标题】:Custom SharePoint WebPart - Custom Property Setting Issue自定义 SharePoint WebPart - 自定义属性设置问题
【发布时间】:2012-06-22 10:38:09
【问题描述】:

我正在开发一个自定义列表搜索工具。我有多个自定义属性来保留搜索查询的列表,以及要在搜索中查询的字段。

示例用户界面

DROPDOWN(列表中的选定字段)TEXTBOX(查询)SEARCHBUTTON

我的问题是,在加载 WebPart 时,“要搜索的字段”属性是在“要搜索的列表”属性之前设置的,这会导致错误,因为我要检查以确保字段确实存在于正确的在将它们添加到下拉列表之前列出。

有没有办法指定加载时 Web 部件属性的设置顺序?

【问题讨论】:

  • 如果您发布了一个代码示例,展示了您的属性设置的方式/位置,那将会很有帮助。
  • stackoverflow 的新功能 - 发布了我的 .cs 文件中的两个属性。它们是文件中的前两个,问题是 listToSearch 是在 searchByOptions 之后设置的,它需要设置中的 listToSearch 属性,而不是默认值
  • 确实如此。将来,知道您应该编辑您的问题以添加信息。您将它们作为答案发布。

标签: sharepoint-2010 web-parts page-lifecycle


【解决方案1】:

我认为在这种情况下,您最容易将验证逻辑从属性设置器中移出并移到另一个方法中(例如CreateChildControls)。通过这样做,您将删除对属性设置顺序的任何依赖。

我的 Web 部件属性中通常没有逻辑(或非常非常少)。我从一开始就在CreateChildControls 中进行了所有验证。然后,如果某些属性的值缺失或无效,我可以抛出异常,或者更常见的是,使用 Web 部件的输出写出描述性消息。

【讨论】:

    【解决方案2】:
        [Category("Search Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true), WebDisplayName("List Name"),
    WebDescription("Enter list name")]
        public string CustomTextProp {
            get { return listToSearch; }
            set {
                int existsFlag = 0;
                foreach (SPList spl in thisWeb.Lists) {
                    if (spl.Title == value || value == string.Empty) {
                        existsFlag = 1;
                        break;
                    }
                }
                if (existsFlag == 1) {
                    listToSearch = value;
                } else {
                    throw new WebPartPages.WebPartPageUserException("The list entered does not exist - Enter an existing list or create a new one");
                }
            }
        }
    

    【讨论】:

      【解决方案3】:
      [Category("Search Settings"),
      Personalizable(PersonalizationScope.Shared),
      WebBrowsable(true), WebDisplayName("Search Field Options (Separate by comma ',')"),
      WebDescription("Enter Fields to Search By")]
          public string SearchByOptions {
              get {
                  return searchByOptions;
              }
              set {//between here
                  //  int validFlag = 1;
                  //  foreach (string str in SeparateByComma(value)) {
                  //    if (!FieldExists(str, CustomTextProp)) {
                  //      validFlag = 0;
                  //      break;
                  //    }
                  //  }
                  //  if (validFlag == 1) {
                  searchByOptions = value;
                  //  } else {
                  //    throw new WebPartPages.WebPartPageUserException("Option is null or one or more fields do not exist/have been entered incorrectly");
                  //  }//and here
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-24
        • 2011-07-10
        • 2011-09-26
        • 2011-06-13
        • 2011-08-23
        • 1970-01-01
        • 2011-12-10
        • 2013-06-22
        相关资源
        最近更新 更多