Ole Albers
Ole Albers

Reputation: 9305

How to Set Properties in Sharepoint dynamically?

I programmed a WebPart for Sharepoint which allows to set some Properties dynamicly.

The Code of this Webparts is the following:

    private void SetValues()
    {
        int counter = 0;
        Control userControl = this.Controls[0];
        for (int i=0; i<userControl.Controls.Count; i++) {
        //foreach (Control element in userControl.Controls) {
            Control element = userControl.Controls[i];
            if (element is Button)
            {
                Button button = (Button)element;
                if (counter < 9)
                {
                    button.Text = _buttonCaptions[counter];
                    element = button;
                }
                counter++;
            }
        }

    }

    #endregion Properties

    #region Methods

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);

        SetValues();
    }

The Properties are shown in Sharepoint and when editing these Values the "SetValues()"-Method is called. But the Buttontexts do not update until I stop the program and recompile the code. Even a website-reload does not help.

When debugging I can see that "button.Text" is assigned correctly.

Any clue?

[edit] Thanks to the answer 2 I changed it the following way:

 protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        SetValues(control);          
        Controls.Add(control);           
   }

Upvotes: 0

Views: 496

Answers (2)

Govind
Govind

Reputation: 544

Did you try adding controls Controls.Add(control) after setvalues method...

Upvotes: 2

Tom Clarkson
Tom Clarkson

Reputation: 16174

Try calling SetValues with control as a parameter before calling Controls.Add - adding the user control to the web part control collection can have some side effects and the code may not execute in the order you expect.

Also, check if you have anything non-default in settings for either the object cache or precompilation.

Upvotes: 0

Related Questions