自定义SharePoint WebPart - 自定义属性设置问题

时间:2012-06-19 20:45:04

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

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

示例用户界面

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

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

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

3 个答案:

答案 0 :(得分:0)

    [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");
            }
        }
    }

答案 1 :(得分:0)

[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
        }
    }

答案 2 :(得分:0)

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

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