0

我已经环顾了一分钟,无法找到如何获得预期的结果。目标:从下拉列表中选择选项之一,标题、NFL、NCAA 或 MLB。在这样做时显示相应的提要。我目前这样做的方法是尝试通过在选择下拉列表项时刷新页面来更改 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)
    {

    }
}
4

1 回答 1

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 的非常有用的文章,这里有一篇,这里有一篇。

希望能帮助到你!

于 2013-10-14T06:14:53.777 回答