![](/img/trans.png)
[英]C# Asp.net Cascading drop-down select lists using Searchable Options List plugin
[英]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.