AspxGridview 不支持所指定的方法 (独立数据访问层)

相信大家刚开始使用ASPxGridView一定遇到这样的情况:

 使用ASPxGridView控件时,点Edit或new按钮,出来Update和cancel,编辑完数据后点击Update,出错:在正在编辑的那行下方会出来一汗背景淡红色字体是红色的字“不支持所指定的方法”,英文是Specified method is not supported.

 这两天正在试用这个控件,被这个问题困扰多时,在网上看到的一些资料,要么感觉处理过程特别繁琐,要么就是省略了我真正关心的内容。

 刚刚处理了这个问题,有点兴奋,也想让被这个问题困扰的朋友们早点解脱,所以记录一下我的一点心得。

 

其实要解决这样问题,非常简单:

1、确保ASPxGridView已设置了KeyFieldName
2、确保ASPxGridView已定义了事件 OnRowDeleting, OnRowInserting, OnRowUpdating
3、后台代码中有对 OnRowDeleting, OnRowInserting, OnRowUpdating 事件的处理。

 

但为什么这么一简单的问题很多人都会遇到呢?

我觉得这应该算是 Vs2008的一个bug,具体表现为:

在vs2008的设计视图上做的一些修改,在代码视图中没能被体现;简单讲就是我们修改了界面,html代码未发生任何变化!

所以我们明明在设计视图中为aspxgridview定义了OnRowDeleting, OnRowInserting, OnRowUpdating 事件,但html代码中却没有,这也正是我们努力了很久都没解决问题的原因,Vs2008骗了我们!#%&#@#……

其实不光这些事件,我发现有时在设计视图增加或修改了AspxGridview的列,代码也同样没有变化。用的是vs2008,不知道在vs2005下会不会有这些问题?如有在vs2005下使用DevExpress控件的朋友,还请说说。

 

以下是我测试时的一个完整的例子,供参考:

1、aspx:

复制代码
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test"%>

<%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace
="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv"%>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace
="DevExpress.Web.ASPxEditors" TagPrefix="dxe"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="CategoryID"
OnRowDeleting
="grid_RowDeleting" OnRowInserting="grid_RowInserting" OnRowUpdating="grid_RowUpdating">
<Columns>
<dxwgv:GridViewCommandColumn VisibleIndex="0">
<EditButton Visible="True">
</EditButton>
<NewButton Visible="True">
</NewButton>
<DeleteButton Visible="True">
</DeleteButton>
</dxwgv:GridViewCommandColumn>
<dxwgv:GridViewDataTextColumn VisibleIndex="1" FieldName="CategoryID" Caption="CategoryID"
ReadOnly
="true">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn VisibleIndex="2" FieldName="CategoryName" Caption="CategoryName">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn VisibleIndex="3" FieldName="Description" Caption="Description ">
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
复制代码

2、cs:

代码

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

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData(grid);
        }
    }

    protected void GetData(ASPxGridView grid)
    {
        grid.DataSource = SqlHelper.ExecuteDataset(System.Data.CommandType.Text, "select * from categories");
        grid.DataBind();
    }

    protected void UpdateData(object CategoryID, object CategoryName, object Description)
    {
        string sql = String.Format("Update categories set CategoryName='{0}',Description='{1}' where CategoryID={2}", CategoryName, Description, CategoryID);
        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text,sql);
    }

    protected void InsertData(object CategoryID, object CategoryName, object Description)
    {
        string sql = String.Format("insert into categories (CategoryName,Description) values ('{0}','{1}')", CategoryName, Description);
        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text,sql);
    }

    protected void DeleteData(object CategoryID)
    {
        string sql = String.Format("delete from categories where CategoryID={0}", CategoryID);
        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text,sql);
    }

    protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        UpdateData(e.OldValues["CategoryID"], e.NewValues["CategoryName"], e.NewValues["Description"]);
        e.Cancel = true;
        (sender as ASPxGridView).CancelEdit();
        GetData((sender as ASPxGridView));

    }

    protected void grid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        DeleteData(e.Keys[0]);
        e.Cancel = true;
        GetData((sender as ASPxGridView));
    }

    protected void grid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        InsertData(e.NewValues["CategoryID"], e.NewValues["CategoryName"], e.NewValues["Description"]);
        e.Cancel = true;
        (sender as ASPxGridView).CancelEdit();
        GetData((sender as ASPxGridView));
    }
}

 

3、db script (SQL Server):

代码

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Categories]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Categories]
GO

CREATE TABLE [dbo].[Categories] (    [CategoryID] [int] IDENTITY (1, 1) NOT NULL ,    [CategoryName] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NOT NULL ,    [Description] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,    [Picture] [image] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GOALTER TABLE [dbo].[Categories] WITH NOCHECK ADD     CONSTRAINT [PK_Categories] PRIMARY KEY  CLUSTERED     (        [CategoryID]    )  ON [PRIMARY] 
GO 

CREATE  INDEX [CategoryName] ON [dbo].[Categories]([CategoryName]) ON [PRIMARY]
GO

insert into categories(CategoryName, Description) select 'Beverages','Soft drinks, coffees, teas, beers, and ales';
insert into categories(CategoryName, Description) select 'Condiments','Sweet and savory sauces, relishes, spreads, and seasonings';
insert into categories(CategoryName, Description) select 'Confections','Desserts, candies, and sweet breads';
insert into categories(CategoryName, Description) select 'Dairy Products','Cheeses';
insert into categories(CategoryName, Description) select 'Grains/Cereals','Breads, crackers, pasta, and cereal';
insert into categories(CategoryName, Description) select 'Meat/Poultry','Prepared meats';
insert into categories(CategoryName, Description) select 'Produce','Dried fruit and bean curd';
insert into categories(CategoryName, Description) select 'Seafood','Seaweed and fish';

 

 
posted @ 2013-04-09 14:45  C#老头子  Views(857)  Comments(0Edit  收藏  举报