0
votes

I am developing a custom list-search tool. I have multiple custom properties to retain which list the search queries, as well as which fields to query in the search.

EXAMPLE UI

DROPDOWN(Selected fields in list) TEXTBOX(Query) SEARCHBUTTON

My issue is that when loading the WebPart, the "Fields to Search" property is being set before the "List to Search" property, which causes and error as I have a check to make sure the Fields actually exist in the correct list before adding them to the dropdown.

Is there any way to designated which order the web part properties get set in on load?

3
If you posted a sample of your code demonstrating how/where your properties are getting set, it would be helpful.CBono
New to stackoverflow - posted the two properties that are in my .cs file. They are the first two in the file and the issue is that the listToSearch is getting set after the searchByOptions which needs the listToSearch property from the settings, not the defaultuser1429393
Indeed. In the future, know that you should edit your question to add information. You posted them as answers.CBono

3 Answers

0
votes

I think in this case, it would be easiest for you to move your validation logic out of your property setters and into another method (CreateChildControls for instance). By doing so, you will remove any dependencies on property-setting order.

I typically have no logic (or very, very little) in my Web Part properties. I do all my validation in/from CreateChildControls right at the very beginning. Then if some property has a missing or invalid value, I can throw an exception or, more typically, write out a descriptive message using the Web Part's output.

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