[英]Labels removed from this.Controls keep reappearing
I am a novice C#
programmer, trying to create a simple RSS/Atom
aggregator webpart for a SharePoint
site (I can't use the ootb part as I don't have an Enterprise licence).我是一名新手
C#
程序员,正在尝试为SharePoint
站点创建一个简单的RSS/Atom
聚合器 Web 部件(我无法使用 ootb 部件,因为我没有企业许可证)。
I have 3 buttons:我有 3 个按钮:
URI
from the user and attempts to construct a label displaying the feed and add it to this.Controls;
URI
,并尝试构造一个 label 来显示该提要并将其添加到this.Controls;
this.Controls
)this.Controls
中删除它们)ArrayList
of default URIs
and converts them each into labels (also adding them to this.Controls
).URIs
的ArrayList
并将它们各自转换为标签(也将它们添加到this.Controls
中)。 Clearing the feeds works fine, as does reverting to the defaults.清除提要工作正常,恢复为默认值也是如此。 However, after clearing all the feeds and trying to add a new feed, the defaults are added as well (and only one new feed can be added at a time, the new one overwriting the old).
但是,在清除所有提要并尝试添加新提要后,也会添加默认提要(一次只能添加一个新提要,新提要覆盖旧提要)。 I suspect this is because I don't fully understand the add/remove controls function.
我怀疑这是因为我不完全了解添加/删除控件 function。 Code follows below:
代码如下:
ArrayList viewedFeeds = new ArrayList();
ArrayList defaultFeeds = new ArrayList(); //Contains several default feeds, e.g. BBC news (http://feeds.bbci.co.uk/news/rss.xml)
private void newFeed_Click(object sender, EventArgs e)
{
renderFeed(userText.Text);
}
private void clearFeeds_Click(object sender, EventArgs e)
{
clearAllFeeds();
}
private void defaultFeeds_Click(object sender, EventArgs e)
{
clearAllFeeds();
initialiseFeedViewer();
}
private void clearAllFeeds()
{
foreach (Label feed in viewedFeeds)
{
this.Controls.Remove(feed);
}
viewedFeeds.Clear();
}
private void initialiseFeedViewer()
{
foreach (string uri in defaultFeeds)
renderFeed(uri);
}
private void renderFeed(String uri)
{
try
{
Label feed = new Label();
// Create a Syndicated feed reader, parse the XML and add the relevant text to the label "feed"
feed.BorderStyle = System.Web.UI.WebControls.BorderStyle.Double;
viewedFeeds.Add(feed);
this.Controls.Add(feed);
}
catch (Exception ex)
{
//Print an error message (e.g. If the URI does not link to a suitable feed
}
}
It turns out I was calling initialiseFeedViewer()
more than once - it was in the createChildControls()
method.事实证明,我不止一次调用了
initialiseFeedViewer()
- 它在createChildControls()
方法中。 I assumed this was only called once, similar to a constructor, but it seems to be called every time the page is loaded.我假设它只被调用一次,类似于构造函数,但似乎每次加载页面时都会调用它。
This has the annoying side effect of clearing all of my variables, so the List<Label>
s I'm using to keep track of displayed feeds are rendered useless.这具有清除所有变量的烦人副作用,因此我用来跟踪显示的提要的
List<Label>
变得无用。
I'll attempt to get around this by storing them in a Sharepoint List instead.我将尝试通过将它们存储在 Sharepoint 列表中来解决此问题。
For you're problem of being unable to add more than one control at a time.因为您无法一次添加多个控件的问题。 Try setting the "Name" property of the label before you try adding it to the arraylist and the "panel" you are using to hold you're label controls.
尝试设置 label 的“名称”属性,然后再尝试将其添加到 arraylist 和用于保存 label 控件的“面板”。
private void renderFeed(String uri)
{
try
{
Label feed = new Label();
feed.Name = uri;
//Create a Syndicated feed reader, parse the XML and add the relevant text to the label "feed"
feed.BorderStyle = System.Web.UI.WebControls.BorderStyle.Double;
viewedFeeds.Add(uri);
this.Controls.Add(feed);
}
catch (Exception ex)
{
//Print an error message (e.g. If the URI does not link to a suitable feed
}
}
As for it rendering your defaults when you add a new one.至于当你添加一个新的时它会呈现你的默认值。 I would just make sure you're not calling the initialiseFeedViewer() on accident.
我只是确保您不会意外调用 initialiseFeedViewer()。
Just as a sidenote, arraylist is considered to be depreciated.顺便说一句,arraylist 被认为是折旧的。 Consider using List instead.
考虑改用 List。
List<string> viewedFeeds = new List<string>();
List<string> defaultFeeds = new List<string>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.