使用下拉列表选择使用ASP.NET的Feed

时间:2013-10-13 22:17:49

标签: c# asp.net rss

我已经四处看了一会儿,但却找不到如何获得预期的结果。 目标:从下拉列表中选择其中一个选项,标题,NFL,NCAA或MLB。 这样做会显示相应的Feed。 我目前执行此操作的方法是尝试通过在选择下拉列表项时刷新页面来更改DataFile属性。 这不一定是必须如何完成的,但那是我无法工作的溃败。

任何建议或指示都会很棒,除了任何微软相关的网页,他们的服务垄断或多或少会阻止他们提供帮助,特别是当我不需要花钱时.... 这是代码: (并且注意到我不打算发布任何此类内容,显然有些人会考虑链接到RSS源,就像我正在试图“窃取”。在这种情况下只是一个类作业。)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="DropDownList2" runat="server" 
            OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" 
            AutoPostBack="true">

            <asp:ListItem>Headlines</asp:ListItem>
            <asp:ListItem>NFL</asp:ListItem>
            <asp:ListItem>NCAA Football</asp:ListItem>
            <asp:ListItem>MLB</asp:ListItem>
        </asp:DropDownList>
        <br />

        <asp:ListView ID="ListView1" runat="server" 
            DataSourceID="XmlDataSource1" 
            OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
            <LayoutTemplate>
                <ul>
                    <asp:PlaceHolder ID="itemPlaceHolder" runat="server">
                    </asp:PlaceHolder>
                </ul>
            </LayoutTemplate>
            <ItemTemplate>
                <li>
                    <a href="<%#XPath("link") %>"> <%#XPath("title") %></a>
                </li>
            </ItemTemplate>
        </asp:ListView>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
            DataFile="http://feeds.feedburner.com/foxsports/rss/headlines" 
            XPath="rss/channel/item">
        </asp:XmlDataSource>

    </div>
    </form>
</body>
</html>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string FeedSource = XmlDataSource1.DataFile;;
        string FeedName = "";

        switch (FeedName)
        {
            case "HHeadlines":
                FeedSource = "http://feeds.feedburner.com/foxsports/rss/headlines";
                break;
            case "NFL":
                FeedSource = "http://feeds.feedburner.com/foxsports/rss/nfl";
                break;
            case "NCAA Football":
                FeedSource = "http://feeds.feedburner.com/foxsports/rss/cfb";
                break;
            case "MLB":
                FeedSource = "http://feeds.feedburner.com/foxsports/rss/mlb";
                break;

        }
    }
    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

1 个答案:

答案 0 :(得分:0)

您需要从下拉列表中获取所选值,并相应地更改XmlDataSource1.DataFile。然后重新绑定listview1:

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    string FeedSource = XmlDataSource1.DataFile; ;
    string FeedName = DropDownList2.SelectedValue;

    switch (FeedName)
    {
        case "HHeadlines":
            FeedSource = "http://feeds.feedburner.com/foxsports/rss/headlines";
            break;
        case "NFL":
            FeedSource = "http://feeds.feedburner.com/foxsports/rss/nfl";
            break;
        case "NCAA Football":
            FeedSource = "http://feeds.feedburner.com/foxsports/rss/cfb";
            break;
        case "MLB":
            FeedSource = "http://feeds.feedburner.com/foxsports/rss/mlb";
            break;

    }
    XmlDataSource1.DataFile = FeedSource;
    ListView1.DataBind();
}

Scott Mitchell撰写了许多关于rss的非常有用的文章,一篇here,另一篇here's

希望它有所帮助!