如何在网格视图中分离刷新率?我正在使用下拉列表来过滤我的数据。如果没有选择它会显示所有这些。例如,它正在与国家/地区进行过滤,我希望gridview按国家/地区使用不同的重新刷新率。
<form id="form1" runat="server">
<h3>Northwind Employees</h3>
<table cellspacing="10">
<tr>
<td valign="top">
<table border="0">
<tr>
<td valign="top">Country</td>
<td><asp:DropDownList runat="server" id="CountryListBox" AppendDataBoundItems="True"
DataSourceID="CountrySqlDataSource"
DataTextField="Country" DataValueField="Country" >
<asp:ListItem Selected="True" Value="" >(Show All)</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Last Name</td>
<td><asp:TextBox runat="server" id="LastNameTextBox" Text="*" /></td>
</tr>
<tr>
<td></td>
<td><asp:Button runat="server" id="FilterButton" Text="Filter Results" /></td>
</tr>
</table>
</td>
<td valign="top">
<asp:GridView ID="EmployeesGridView"
DataSourceID="EmployeeDetailsSqlDataSource"
AutoGenerateColumns="false"
AllowSorting="true"
DataKeyNames="EmployeeID"
Gridlines="Both"
RunAt="server">
<HeaderStyle backcolor="Navy"
forecolor="White"/>
<RowStyle backcolor="White"/>
<AlternatingRowStyle backcolor="LightGray"/>
<EditRowStyle backcolor="LightCyan"/>
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>
<asp:BoundField DataField="FirstName" HeaderText="First Name"/>
<asp:BoundField DataField="LastName" HeaderText="Last Name"/>
<asp:BoundField DataField="Country" HeaderText="Country"/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="CountrySqlDataSource"
SelectCommand="SELECT DISTINCT Country FROM Employees"
EnableCaching="True"
CacheDuration="60"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
RunAt="server" />
<asp:SqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="SELECT EmployeeID, LastName, FirstName, Country FROM Employees"
EnableCaching="True"
CacheDuration="60"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
FilterExpression="Country LIKE '{0}' AND LastName LIKE '{1}'"
RunAt="server">
<FilterParameters>
<asp:ControlParameter ControlID="CountryListBox" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="LastNameTextBox" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
</form>
答案 0 :(得分:2)
好吧,我认为您不能部分呈现GridView
数据,但我只是使用UpdatePanel
和Timer
控件进行了实验,它运行良好,网格数据更新,因为我在服务器上进行分页,只需从SQL Server中检索一小部分数据。
好处是当数据更新时保留当前页面索引和顺序,所以实际上看起来如果网格数据将部分更新
看看:
<asp:ScriptManager runat="server" />
<asp:ObjectDataSource runat="server" ID="ods" TypeName="MyObject" EnablePaging="True"
SelectMethod="FindPaging"
MaximumRowsParameterName="pageSize"
SortParameterName="sortColumn"
StartRowIndexParameterName="startIndex"
SelectCountMethod="FindPagingCount"
>
<SelectParameters>
<asp:Parameter Name="sortColumn" Type="String" />
<asp:Parameter Name="startIndex" Type="Int32" />
<asp:Parameter Name="pageSize" Type="Int32" />
<asp:ControlParameter Name="job" Type="String" ControlID="ddl" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList AutoPostBack="true" runat="server" ID="ddl" DataTextField="job_desc" DataValueField="job_id">
</asp:DropDownList>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="grid" runat="server" AllowPaging="True" AllowSorting="True" PageSize="10"
DataSourceID="ods" AutoGenerateColumns="true">
</asp:GridView>
<asp:Label runat="server" ID="lblMessage"></asp:Label>
<asp:Timer Enabled="true" Interval="5000" runat="server" ID="timer" OnTick="timer_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.ddl.DataSource = new DataClassesDataContext().jobs;
this.ddl.DataBind();
}
}
protected void timer_Tick(object sender, EventArgs e)
{
this.grid.DataBind();
}
public class MyObject
{
public IEnumerable<employee> FindPaging(string job, int startIndex, int pageSize, string sortColumn)
{
var c = new DataClassesDataContext();
var sort = string.Empty;
if (string.IsNullOrWhiteSpace(sortColumn) || sortColumn == "sortColumn")
{
sort = "fname";
}
else
{
sort = sortColumn.Replace("sortColumn ", string.Empty);
sort = sort.Replace(" DESC", string.Empty);
}
var q = c.employees.Where(x => x.job_id.ToString() == job).Skip(startIndex).Take(pageSize);
if (!sortColumn.Contains("DESC"))
{
return q.OrderBy(sort);
}
else
{
return q.OrderByDescending(sort);
}
}
public int FindPagingCount(string job, int startIndex, int pageSize, string sortColumn)
{
var c = new DataClassesDataContext();
return c.employees.Where(x => x.job_id.ToString() == job).Count();
}
}