我有一个非常简单的带有listview的asp.net webform页面,我无法在回发时刷新listView,只有在我发出新请求时才会这样做。我想listView从viewstate或其他东西获取它的内容。我在Page_Load事件中进行了数据绑定,并且我也尝试了Page_Init事件。没有区别。
我可能已经浏览了前50个Google搜索结果,但似乎没有任何效果。
如何强制列表视图在回发时刷新?
Default.aspx的
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RSS_Reader.Default" %>
<%@ Import namespace="RSS_Reader.Extensions" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RSS Reader</title>
<link rel="shortcut icon" href="favicon.ico" />
<link href="css/main.css" rel="stylesheet" />
<script src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="js/App.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="horizontal-menu">
<ul id="horizontal-menu-list">
<li id="add-feed">Add Feed
<ul>
<li>
<input type="text" placeholder="Feed URL" /><button class="button green">Add</button>
</li>
</ul>
</li>
<li>Add Group</li>
<li>Settings</li>
</ul>
<div id="global-search">
<input type="text" id="search" placeholder="Search" />
</div>
<div style="clear: both"></div>
</div>
<div id="feeds">
<div id="feed-filter">
<asp:TextBox ID="feedUrl" runat="server" placeholder="Feed URL"></asp:TextBox>
<asp:Button ID="ThisIsAUniqueNameForAButton" runat="server" CssClass="button green" Text="Subscribe" OnClick="subscribeToFeed" />
</div>
<div id="feed-wrapper">
<asp:ListView ID="FeedCollection" runat="server">
<ItemTemplate>
<div><div class="feed-collection-icon"></div><asp:Label runat="server" ID="listFeedTitle" Text='<%# Bind("Title") %>'></asp:Label></div>
</ItemTemplate>
</asp:ListView>
</div>
</div>
<div id="content">
<div id="feed-title">Version2 - IT For Professionelle</div>
<div id="nodes">
<asp:GridView ID="nodesGridview" OnSorting="nodesGridview_Sorting" runat="server" OnSortCommand="SortGrid" AllowSorting="True" AutoGenerateColumns="false">
<EmptyDataTemplate>
No feed.
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Title" SortExpression="title">
<ItemTemplate>
<div class="node-icon"></div> <%# XPath("title") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="description">
<ItemTemplate>
<%# XPath("description").ToString().Truncate(50) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="pubDate">
<ItemTemplate>
<%# XPath("pubDate").ToString().toDate() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div id="reading-pane">
<asp:Label ID="readingPaneTitle" runat="server" Text="Label"></asp:Label>
</div>
</div>
<div id="footer">
Copyright © 2014 - Mads O. Nielsen
</div>
</div>
</form>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Diagnostics;
using System.Data.Entity;
using System.Data;
using System.IO;
using RSS_Reader.FeedCollection;
using RSS_Reader.FeedItem;
namespace RSS_Reader
{
public partial class Default : System.Web.UI.Page
{
private XmlDataSource feedItems;
private FeedReader feedReader;
protected void Page_Init(object sender, EventArgs e)
{
Debug.WriteLine("Page_Init");
if (!IsPostBack)
{
}
}
protected void Page_Load(object sender, EventArgs e)
{
RSS_Reader.Model db = new RSS_Reader.Model();
//var feedQuery = from emp in db.Feeds select emp;
//List<Feeds> empList = feedQuery.ToList();
FeedCollection.DataSource = db.Feeds.ToList();
feedReader = new FeedReader();
feedReader.load("http://www.version2.dk/it-nyheder/rss");
feedItems = new XmlDataSource();
feedItems.EnableCaching = false;
feedItems.ID = "feedItems";
feedItems.Data = feedReader.feed.OuterXml;
feedItems.XPath = "rss/channel/item";
nodesGridview.Width = new Unit(100.00, UnitType.Percentage);
nodesGridview.DataSource = feedItems;
FeedCollection.DataBind();
nodesGridview.DataBind();
}
protected void subscribeToFeed(object sender, EventArgs e)
{
FeedManager feedManager = new FeedManager();
feedManager.subscribe(feedUrl.Text);
}
protected void nodesGridview_Sorting(object sender, GridViewSortEventArgs e)
{
Debug.WriteLine("Sorting");
}
}
}
答案 0 :(得分:0)
如果由于将其保存到ViewState而无法刷新ListView,则可以将EnableViewState属性设置为False,然后服务器在所有回发中刷新它