如何动态设置Sharepoint中的属性?
我为 Sharepoint 编写了一个 WebPart,它允许动态设置一些属性。
该 Web 部件的代码如下:
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();
}
属性显示在 Sharepoint 中,编辑这些值时将调用“SetValues()”方法。但在我停止程序并重新编译代码之前,按钮文本不会更新。即使重新加载网站也无济于事。
调试时我可以看到“button.Text”已正确分配。
有什么线索吗?
[编辑] 感谢答案2,我将其更改为以下方式:
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
SetValues(control);
Controls.Add(control);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试在 setvalues 方法之后添加控件
Controls.Add(control)
...Did you try adding controls
Controls.Add(control)
after setvalues method...在调用 Controls.Add 之前,尝试使用控件作为参数调用 SetValues - 将用户控件添加到 Web 部件控件集合可能会产生一些副作用,并且代码可能不会按您期望的顺序执行。
另外,检查对象缓存或预编译的设置中是否有任何非默认设置。
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.