簡體   English   中英

使用下拉列表選擇使用ASP.NET的供稿

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

我環顧了好一會兒,卻找不到如何獲得預期的結果。 目標:從下拉列表中,選擇“標題”,“ NFL”,“ NCAA”或“ MLB”之一。 這樣做后,顯示相應的提要。 我當前執行此操作的方法是嘗試通過在選擇下拉列表項時刷新頁面來嘗試更改DataFile屬性。 這不一定是必須要做的,但這就是我無法工作的結果。

任何建議或指示都將是很好的,但對於任何與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>

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

    }
}

您需要從下拉列表中獲取選定的值,並相應地更改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的非常有用的文章, 這里有一篇, 這是另一篇。

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM