【问题标题】:ASPX Codebehind - Changing SelectCommand causes sorting to revert to original dataASPX Codebehind - 更改 SelectCommand 会导致排序恢复为原始数据
【发布时间】:2017-06-29 11:29:49
【问题描述】:

我有一个附加到 SQLDatasource 的 GridView,它工作正常,我可以使用原始数据对其进行正确排序。

如果我告诉代码隐藏通过 ASPX 页面上的预定义 SQLDataSource (SqlDataSource2) 查找特定信息,则后续排序正常工作。

如果我告诉代码隐藏修改预先存在的 sqldatasource 并绑定它,后续排序将无法正常工作,但会返回原始数据。

先决条件: 在 Web.config 中定义的 SQL 数据源连接字符串,在表 document_index 中具有 id、last_modified 和 view_count 列。

ASPX:

 <%@ 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">
           <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />


        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            SelectCommand="SELECT id, last_modified, view_count FROM [document_index] ORDER by id" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" /> 
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            SelectCommand= "SELECT id, last_modified, view_count FROM [document_index] WHERE view_count LIKE '7'"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" /> 
<div>
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
                    <asp:Button runat="server" Text="Select View Using ASPX SQL Source2 (Sort Works)" OnClick="aspxviewchange" />
                    <asp:Button runat="server" Text="Select View Using Codebehind SQL Source1 Change Data (Sort Breaks)" OnClick="cbviewchange" />

            <asp:GridView ID="GridView1" EnableSortingAndPagingCallbacks= "true" runat="server" AllowSorting="True" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" Width="100%" CellPadding="4" Padding="20" DataKeyNames="id" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="25" AllowCustomPaging="True">

                <Columns>
                </Columns>
            </asp:GridView>

        </ContentTemplate>
    </asp:UpdatePanel>
    </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)
    {
        if (!IsPostBack)
        {
            CreateNewColumn("last_modified", "Last Modified");
            CreateNewColumn("view_count", "Views");
        }
    }
    protected void CreateNewColumn(string SQLcolname, string header)
    {
        BoundField NewColumnName = new BoundField();
        NewColumnName.DataField = SQLcolname;
        NewColumnName.SortExpression = SQLcolname;
        NewColumnName.HeaderText = header;
        GridView1.Columns.Add(NewColumnName);
    }
    protected void aspxviewchange(object sender, EventArgs e)  // SUBSEQUENT SORTING WORKS
    {
        GridView1.DataSourceID = "SQLDataSource2";   
        GridView1.DataBind();
    }
    protected void cbviewchange(object sender, EventArgs e) // SUBSEQUENT SORTING BROKEN
    {
        SqlDataSource1.SelectCommand = "SELECT id, last_modified, view_count FROM [document_index] WHERE view_count LIKE '7'";
        GridView1.DataSourceID = "SQLDataSource1";
        GridView1.DataBind();
    }
}

选择 ASPX SQL 按钮可正确排序,随后的排序工作。 选择 Codebehind SQL 按钮排序正确,但后续排序不起作用。

【问题讨论】:

  • 您没有发布足够的代码。基本上它取决于这段代码在页面事件生命周期中的执行位置,这是陈旧且难以解释的。如果可能的话,我建议您将整个代码发布在后面。
  • 对不起,我编辑了我的原始帖子并发布了完整的代码。

标签: c# asp.net .net gridview sqldatasource


【解决方案1】:

但后续排序不起作用

后续排序不起作用的原因是您正在重新提交页面,并且 Grid 正在重新绑定到默认数据源 (DataSourceID="SqlDataSource1")。

您必须捕获“发送”排序请求的数据源并重新绑定网格

类似:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)  
    {
         //capture SORT request (which View?)
         string RequestType = (get the grid request type);  
         if(RequestType  = "SortView1")
         {
             GridView1.datasourceID = datasource1; 
             GridView1.DataBind();
         }
         else if(RequestType  = "SortView2")
         {
             GridView1.datasourceID = datasource2; 
             GridView1.DataBind();
         }

    }
    else
    {
       GridView1.datasourceID = datasource1; 
       GridView1.DataBind();
    }
}

或者,您可以(重写)编写自己的“onGridView1Submit”方法

无论哪种方式,您还应该删除网格控件中的默认数据源DataSourceID="SqlDataSource1"

【讨论】:

  • ASPX GridView 没有 Rebind 属性,所以 GridView1.Rebind();不管用。除非我需要使用额外的库,如果需要,请详细说明。
  • 是的,你是对的。我只是习惯于公开 Rebind() 方法(对于 asp.net Grids)的第三方供应商。最重要的是,只是想传达导致您所看到的行为的逻辑。这是 Webforms 中最棘手的部分之一。习惯事件发生的时间和顺序。我添加了更多代码,但没有编写所有条件逻辑来获取发出了哪个排序请求,但是如果您自己填写该代码,请告诉我们。我会尽力提供帮助。祝你好运!
  • 这对你有用吗?如果需要任何进一步的帮助,请告诉我?
  • 不完全,例如,我不确定要放入占位符“(获取网格请求类型)”中的代码。而且,我希望 datasourceid 在技术上始终与 SQLDataSource1 绑定,仅使用不同的选择命令参数,但这样做会导致排序问题。
  • 您发布的代码表明您正在尝试动态重置数据源。重置 Select 参数是更好的方法。如果你想走那条路,你可以在你的按钮上添加“CommandName”和“CommandValue”
【解决方案2】:

我决定根据我的代码隐藏参数在单个 SQL 语句中使用 IF/BEGIN/END/ELSE IF 子句来解决此问题。考虑到动态更改选择命令会导致其他问题,这似乎是最好的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2018-01-15
    • 2013-06-11
    相关资源
    最近更新 更多