ASP.NET Dynamic Data Site: Force First Column to be Alphabetized

戏子无情 提交于 2019-12-11 05:52:40

问题


I have deployed a ASP.NET Dynamic Data Site here: https://ess.orthman.com/PhoneListWeb/

The Columns can be alphabetized by clicking on the Column name, but how can I set the site to automatically alphabetize that first column?


回答1:


You have a number of options available, depending on which machine you want the burden of ordering.

1. You can create a Stored Procedure on the DB server which will SELECT * FROM ... ORDER BY ... and use that in your .dbml

2. You mentioned you are using Linq to SQL classes generated from a .dbml, so I am going on the assumption you are using LinqDataSource in your .aspx pages.

From within your designer, you can select Configure Data Source on your LinqDataSource:

Then you choose your Context from your .dbml, and on the next screen you have the option to Order By:

3. Using Dynamic Data Web Site you do not have the advantage of a specific table structure at design time. Because of this, you need to create a sort that will happen at runtime. You can edit your DynamicData\PageTemplates\List.aspx.cs to include the following:

protected void Page_Load(object sender, EventArgs e)
{
    Title = table.DisplayName;

    // Disable various options if the table is readonly
    if (table.IsReadOnly)
    {
        GridView1.Columns[0].Visible = false;
        InsertHyperLink.Visible = false;
        GridView1.EnablePersistedSelection = false;
    }

    // Add our sort to the first data column.
    if (!Page.IsPostBack)
    {
        GridView1.Sort(table.Columns[0].Name, SortDirection.Ascending);
    }
}



回答2:


First and simple answer will be sorting your data sources
Using order by before fetching data (form DB) or linq sort objects after fetching them ...




回答3:


After Binding the Data From dbml You might have a linq statement to bind Data into a Grid or List.

Code something similar to the Same.

In this Below sample code, I have just added sorting on binding the data

Order by COMPANY NAME ASCENDING

var Company = from Company in _c.Company_Name
             orderby _c.Company_Name 
             select Company;

Order by COMPANY NAME DESCENDING

var Company = from Company in _c.Company_Name
             orderby _c.Company_Name descending
             select Company;

Try this Hope it will help




回答4:


You can use the DisplayColumnAttribute to designate the column that should be used for sorting. There is a simple and easy way to sort and there is a more complex and powerful approach. I will give you both.

The simple way first, showing an example of how to sort an entity. In this example the PostalCode column from the Address table (the parent table) is used for sorting the Address.

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

[DisplayColumn("City", "PostalCode", false)]
public partial class Address
{

}

If your sorting needs are more complex then try this more robust approach from the C# Bits blog: Setting the initial sort order

This link documents the DisplayColumnAttribute.




回答5:


If you are using ASP.NET 4.0 you can consider GridView's AllowSorting = true with new sorting-related style properties SortedAscendingHeaderStyle and other.

Example of .ASPX:

<asp:GridView 
    ID="gvOffices" 
    runat="server" 
    DataSourceID="GridDataSource"
    AllowPaging="true" 
    AllowSorting="true" 
    PageSize="10" 
    CssClass="listtable" 
    AutoGenerateColumns="false"
    EnablePersistedSelection="true"
    OnSelectedIndexChanged="OnFilterSelectedIndexChanged"
    SortedAscendingHeaderStyle-CssClass="sortasc-header" 
    SortedDescendingHeaderStyle-CssClass="sortdesc-header">
</asp:GridView>

Example of .CSS:

/*#region Table Header Sort Image */
.sortasc-header a {
    background: url(Images/arrowup.gif) right center no-repeat;
}

.sortdesc-header a {
    background: url(Images/arrowdown.gif) right center no-repeat;
}
/*#endregion Table Header Sort Image */

AllowSorting prompts the GridView to render its header row using LinkButton controls that, when clicked, cause a postback and initiate the sorting process. And new properties allow to define the visual appearance of the column header depending on current order.

More information you can find at MSDN.

EDIT:

If you would like to sort only by the first column, you should set SortExpression property of DynamicField control of the other columns to blank.

<asp:DynamicField DataField="ListCity" HeaderText="City" SortExpression="" />


来源:https://stackoverflow.com/questions/15391762/asp-net-dynamic-data-site-force-first-column-to-be-alphabetized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!