简体   繁体   English

从 this.Controls 中删除的标签不断重新出现

[英]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 个按钮:

  1. New Feed, which takes a URI from the user and attempts to construct a label displaying the feed and add it to this.Controls; New Feed,它从用户那里获取一个URI ,并尝试构造一个 label 来显示该提要并将其添加到this.Controls;
  2. Clear Feeds, which clears all the displayed feeds (removing them from this.Controls )清除提要,清除所有显示的提要(从this.Controls中删除它们)
  3. Default Feeds, which takes an ArrayList of default URIs and converts them each into labels (also adding them to this.Controls ).默认提要,它采用默认URIsArrayList并将它们各自转换为标签(也将它们添加到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.

相关问题 this.Controls返回null - this.Controls returns null this.Controls不包含所有控件 - this.Controls doesn't contain all controls C#说this.Controls为null,为什么呢? - C# says this.Controls is null, why? 用XmlNodeList填充“ this.Controls [controlName]”组合框 - Populate a “this.Controls[controlName]” comboBox with XmlNodeList 如何在WPF中使用foreach(this.Controls中的控件c) - How to use foreach (Control c in this.Controls) in WPF 找不到 this.Controls - 我错过了什么? - Can't find this.Controls - what am I missing? 使用字符串作为名称控制/使用 ImageList,例如按钮和 picbox 等:this.Controls[string] - Control/Use ImageList using a string as name, like for buttons and picbox, etc: this.Controls[string] 想要在WPF应用中添加SWF文件! 但是this.Controls无法正常工作..但是已经在参考中添加了它 - want to add swf file in wpf app! but this.Controls cant work..but already added in reference for it 如何跟踪一组控件中的任何控件中的编辑 - How to keep track of editing in any of the controls from a group of controls 为什么在循环内更改 child.Parent 属性时,foreach over this.Controls 集合无法正常工作 - Why foreach over this.Controls collection does not work correctly when child.Parent property changed inside the loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM