简体   繁体   English

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

[英]Use a drop down list to select feed using ASP.NET

I have looked around for a good minute and cannot find out how to get the intended results. 我环顾了好一会儿,却找不到如何获得预期的结果。 The goal: From the drop down list, select one of the options, Headlines, NFL, NCAA, or MLB. 目标:从下拉列表中,选择“标题”,“ NFL”,“ NCAA”或“ MLB”之一。 Upon doing so display the corresponding feed. 这样做后,显示相应的提要。 My current method of doing this is to attempt to change the DataFile attribute by refreshing the page upon selecting a drop down list item. 我当前执行此操作的方法是尝试通过在选择下拉列表项时刷新页面来尝试更改DataFile属性。 This is not necessarily how it has to be done, but that's the rout I couldn't get to work. 这不一定是必须要做的,但这就是我无法工作的结果。

Any advice or pointers would be great, except to any microsoft related pages on account that their service monopoly more or less prevents them from being helpful, especially when I don't have to fork over money.... Here is the code: (and a note that I don't intend to publish any of this, apparently some consider linking to RSS feeds like i'm trying to 'stealing.' Just a class assignment in this case). 任何建议或指示都将是很好的,但对于任何与Microsoft相关的页面,由于它们的服务垄断或多或少会阻止它们提供帮助,尤其是在我不必花钱的时候。...以下是代码:(请注意,我不打算发布任何内容,显然有些人考虑链接到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>

The C# 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)
    {

    }
}

You need to get the selected value from dropdownlist and change the XmlDataSource1.DataFile accordingly. 您需要从下拉列表中获取选定的值,并相应地更改XmlDataSource1.DataFile。 Then rebind the listview1: 然后重新绑定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 wrote many very helpful articles about rss, one here , here's another one. 斯科特·米切尔Scott Mitchell)写了许多关于rss的非常有用的文章, 这里有一篇, 这是另一篇。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM