简体   繁体   中英

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).

I have 3 buttons:

  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;
  2. Clear Feeds, which clears all the displayed feeds (removing them from this.Controls )
  3. Default Feeds, which takes an ArrayList of default URIs and converts them each into labels (also adding them to 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. 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. 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.

I'll attempt to get around this by storing them in a Sharepoint List instead.

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.

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.

Just as a sidenote, arraylist is considered to be depreciated. Consider using List instead.

List<string> viewedFeeds = new List<string>();
List<string> defaultFeeds = new List<string>();

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