简体   繁体   中英

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

您是否尝试在setvalues方法之后添加控件Controls.Add(control) ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM