【问题标题】:ASPxGridView not works in my ASP.NET MVC solutionASPxGridView 在我的 ASP.NET MVC 解决方案中不起作用
【发布时间】:2012-11-23 10:41:28
【问题描述】:

我正在使用ASPxGridView 并使用来自 DataTable 的数据进行更新,工作正常。 但是当我尝试使用编辑、更新、删除、分页、排序功能时,我遇到了麻烦。 显示按钮,但单击时功能不起作用。

这是我的Sample.aspx 文件:

[ASPx]

<dx:aspxgridview ID="grid" ClientInstanceName="grid" runat="server" 
        OnDataBinding="grid_DataBinding" Width="100%" EnableCallBacks="false">        
    
    <Settings ShowTitlePanel="True" />
    <SettingsText Title="Tabla" />
    <SettingsPager PageSize ="10" ShowSeparators="true" PageSizeItemSettings-Items="20" >
        <PageSizeItemSettings Visible="true" />            
    </SettingsPager>
    
</dx:aspxgridview>

Sample.aspx.cs 背后的代码:

[C#]

using System;
using System.Data;
using System.Web.Mvc;
using DataConnector;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

namespace Sample.Views.Actions

{

    public partial class Sample: ViewPage
    {

        private DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                grid.DataBind();

            grid.RowValidating += new ASPxDataValidationEventHandler(grid_RowValidating);

            if (!this.IsPostBack)
            {
                GridViewCommandColumn c = new GridViewCommandColumn();
                grid.Columns.Add(c);                
                c.Caption = "Operations";

                c.EditButton.Visible = true;
                c.UpdateButton.Visible = true;
                c.NewButton.Visible = true;
                c.DeleteButton.Visible = true;
                c.CancelButton.Visible = true;
            }            
        }        

        DataTable BindGrid()
        {
            DataConnection DM = new DataConnection();
            if (DM.login("ADMIN", "PASS"))
            
                dt = DM.getDataSet("SELECT * FROM Table_Name");
                grid.DataSource = dt;
                dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };       
            
            return dt;
        }

        protected void grid_DataBinding(object sender, EventArgs e)
        {
            // Assign the data source in grid_DataBinding
            BindGrid();
        }

        protected void grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
        {
            int id = (int)e.OldValues["id"];
            DataRow dr = dt.Rows.Find(id);
            dr[0] = e.NewValues[""];
            dr[1] = e.NewValues[""];
            dr[2] = e.NewValues[""];
            dr[3] = e.NewValues[""];
            dr[4] = e.NewValues[""];

            ASPxGridView g = sender as ASPxGridView;
            UpdateData(g);
            g.CancelEdit();
            e.Cancel = true;
        }


        void grid_RowValidating(object sender, ASPxDataValidationEventArgs e)
        {
            int id = (int)e.NewValues["id"];
            if ((!e.OldValues.Contains("id") || ((int)e.OldValues["id"] != id))
                && (dt.Rows.Find(id) != null))
            {
                ASPxGridView grid = sender as ASPxGridView;
                e.Errors[grid.Columns["id"]] = String.Format("Column 'Id' is constrained to be unique.  Value '{0}' is already present.", id);
            }
        }


        private void UpdateData(ASPxGridView g)
        {
            g.DataBind();
        }
    }}

在控制器页面上调用示例:

[C#]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DataConnector;

namespace Sample.Controllers

{    
public class ActionsController : Controller

    {
        
        public ActionResult Sample()
        {
            return View();
        }

    }
}

Grid 上显示的数据。

我创建了一个新的解决方案,其中只有 Sample.aspxSample.aspx.cs,并且我在没有 MVC 的情况下使用并且功能正在运行。区别在于“公共部分类样本:System.Web.UI.Page”。 如果我在提到的代码上尝试不使用System.Web.UI.Page,它会给我以下错误:

The view must derive from ViewPage, ViewPage<TModel>, ViewuserControl,or ViewuserControl<TModel>.

我做错了什么?为什么 ASPxGridView 的功能不起作用?

如果我的母语不是英语,我很抱歉,我是一个新程序员:)

【问题讨论】:

    标签: asp.net-mvc datatable aspxgridview


    【解决方案1】:

    ASPxGridView 是一个 WebForms 控件,不能在 ASP.NET MVC 中使用。相反,您应该使用DevExpress MVC extensions。具体来说,DevExpress MVC GridView extension

    关注我的"Getting started with DevExpress MVC video" 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多