配置ObjectDataSource以进行排序

时间:2011-02-02 14:25:29

标签: asp.net gridview objectdatasource aspxgridview

System.NotSupportedException was unhandled by user code
  Message=The data source does not support sorting.
  Source=namespace
  StackTrace:
       at namespace.Admin.ToolkitScriptManager1_AsyncPostBackError(Object sender, AsyncPostBackErrorEventArgs e) in C:\project\Master.cs:line 27
       at System.Web.UI.ScriptManager.OnAsyncPostBackError(AsyncPostBackErrorEventArgs e)
       at System.Web.UI.PageRequestManager.OnPageError(Object sender, EventArgs e)
       at System.Web.UI.TemplateControl.OnError(EventArgs e)
       at System.Web.UI.Page.HandleError(Exception e)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:



  protected void ToolkitScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            ToolkitScriptManager1.AsyncPostBackErrorMessage = Resources.Strings.ErrorAsyncPostBack;
            logger.Error("An asyncpostbackerror has occurred: " + e.Exception.Message);
            throw e.Exception;  // <<<<<< throw error here
        }


  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
         DataObjectTypeName="mynamespace.Product" SelectMethod="GetAllProducts" SortParameterName="sortedBy" 
          TypeName="mynamespace.Products"  UpdateMethod="Update">              
           <SelectParameters>
                <asp:Parameter Name="sortedBy" DefaultValue="DisplayOrdinal, Name" /> 
            </SelectParameters>   
           </asp:ObjectDataSource>

下面是我的代码用于简单的gridview,我只有排序问题和分页工作正常,我甚至定义了ONSorting事件,但它仍然给我一个错误:The data source does not support sorting.

namespace mynamespace
{
public class Product
{
private int _id;

public int Id
{
get { return _id; }
set { _id = value; }
}
private string _code;

public string Code
{
get { return _code; }
set { _code = value; }
}
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}


<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id" 
AutoGenerateColumns="False" OnSorting="gridview1_Sorting" DataSourceID="ObjectDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" 
SortExpression="Id" ReadOnly="True" />
<asp:BoundField DataField="Code" HeaderText="Code" 
SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" 
SortExpression="Name" />
<asp:TemplateField HeaderText="State/Province" SortExpression="StateProvinceName"
                    HeaderStyle-Wrap="true" HeaderStyle-Width="80px">
                    <ItemTemplate>
                        <%# ((namespace)(Container.DataItem)).StateProvince.Name.ToString()%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlState" runat="server" DataSourceID="StateDataSource" DataTextField="Name"
                            DataValueField="StateProvinceId" AutoPostBack="false">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>

</Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
DataObjectTypeName="mynamespace.Product"
SelectMethod="GetAllProducts" TypeName="mynamespace.Products" 
UpdateMethod="Update">
</asp:ObjectDataSource>




protected void gridview1_Sorting(object sender, GridViewSortEventArgs e)
        { 
            if (e.SortExpression == SortField)
            {
                if (SortDirection != null)
                {
                    if (SortDirection == "ASC")
                        SortDirection = "DESC";
                    else
                        SortDirection = "ASC";
                }
                else
                {
                    if (e.SortDirection == System.Web.UI.WebControls.SortDirection.Ascending)
                        SortDirection = "ASC";
                    else
                        SortDirection = "DESC";
                }
            }
            else
            {
                if (e.SortDirection == System.Web.UI.WebControls.SortDirection.Ascending)
                    SortDirection = "ASC";
                else
                    SortDirection = "DESC";
            }
            SortField = e.SortExpression; 
            DataBind(); 
        }


  protected override void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "ObjectDataSource1";

        }


 protected void somecustomlinks_ItemCommand(object source, EventArgs e)
        {
            DataBind();
        }

public override void DataBind()
{
  List<Organization> _org = new List<Organization>();
  Gridview1.DataSourceID = string.Empty;
  Gridview1.DataSource = _orgFilter;
  Gridview1.DataBind();

}

1 个答案:

答案 0 :(得分:0)

为了支持使用ObjectDataSource进行排序,业务对象的Select方法(在您的情况下为GetAllProducts())应该采用包含要应用的排序表达式的参数。

您还必须将ObjectDataSource的{​​{3}}属性设置为该参数的名称。