【问题标题】:ASP.NET Gridview Sorting not functioningASP.NET Gridview 排序不起作用
【发布时间】:2018-04-11 11:41:40
【问题描述】:

我正在尝试使用 asp.net 在 Web 应用程序上实现 gridview。在通过一种方法对我的数据网格进行排序时,我遇到了一个问题——我想征求意见。这是我的 .aspx 文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="GridViewDemo1.Employee" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView 
        ID="grvEmployee" 
        runat="server" 
        AutoGenerateColumns="true"
        BackColor="AliceBlue"
        ForeColor="Goldenrod" 
        BorderColor="YellowGreen"
        BorderStyle="Groove"
        Width="70%"
        CellPadding="3"
        CellSpacing="2" 
        AllowSorting="True"
        OnSorting="GridView1_Sorting" 
        AutoGenerateEditButton="true" 
        AutoGenerateDeleteButton="true" ViewStateMode="Enabled">  


        <RowStyle 
            HorizontalAlign="Center">
        </RowStyle>

        <FooterStyle
            ForeColor="#8C4510"
            BackColor="#F7DFB5">
        </FooterStyle>

        <PagerStyle 
            ForeColor="#8C4510" 
            HorizontalAlign="Center">
        </PagerStyle>

        <HeaderStyle 
            ForeColor="White" 
            Font-Bold="True" 
            BackColor="#A55129">
        </HeaderStyle>

    </asp:GridView>
</div>
</form>

这是.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace GridViewDemo1
{
public partial class Employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        string selectSQL = "SELECT * from dbo.[User]";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "Employee");

        grvEmployee.DataSource = ds;
        grvEmployee.DataBind();

    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //dataTable.DefaultView.Sort = e.SortExpression;
        //grvEmployee.DataSource = dataTable;
        grvEmployee.DataBind();
    }


}
}

这是我的 web.config 连接字符串:

  <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=xxxxxx;Initial Catalog=yyyyyyy;User ID=zzzzz;Password=xxxxxx;" providerName="System.Data.SqlClient" />

gridview 被正确填充,我曾经得到“未处理的触发事件排序”。但现在我试图对列进行排序没有得到任何回应。这甚至适用于自动生成的列吗?我在哪里可以指定排序表达式? (升序/降序等)?

【问题讨论】:

标签: asp.net sorting gridview


【解决方案1】:

只有在!IsPostback 不是每次连续回发时,您才必须执行初始数据绑定:

if(!IsPostBack)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    string selectSQL = "SELECT * from dbo.[User]";
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adapter.Fill(ds, "Employee");

    grvEmployee.DataSource = ds;
    grvEmployee.DataBind();
}

GridView1_Sorting中,你必须从数据库中选择有序数据并将其分配给网格的DataSource属性,然后调用grvEmployee.DataBind()

GridView排序示例:https://stackoverflow.com/a/6602125/284240

【讨论】:

  • 所以我需要为每一列创建不同的排序方法吗?此外,当我单击列时,通过此编辑,页面完全变为空白。抱歉给您添麻烦了,在 asp.net 上真的很新。
  • @onlyf:为什么每列使用一种方法?您的方法中已经有使用e.SortExpression 的代码。但是,stackoverflow 上有很多问题,它们展示了如何使用 ASP.NET GridView 实现排序。我前段时间也提供了一个:stackoverflow.com/a/6602125/284240
猜你喜欢
  • 2017-07-29
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多