1

System.Web.Ui.WebControls.GridView から継承する ASP.NET Web カスタム コントロールを開発しています。

ソースコードは次のとおりです。

 public sealed class FaqListEditor : GridView
{
    private readonly string _connectionString;
    private readonly EntityDataSource _faqEntityDataSource = new EntityDataSource();

    public FaqListEditor()
    {
        _connectionString = ConfigurationManager.ConnectionStrings["FaqEntities"] != null
                                ? ConfigurationManager.ConnectionStrings["FaqEntities"].ConnectionString
                                : null;

        this.InitilizeDataSource();

        this.InitializeGrid();
        this.InitializeGridColumns();
        this.InitializeGridEvents();
    }

    private void InitilizeDataSource()
    {
        this._faqEntityDataSource.AutoPage = true;
        this._faqEntityDataSource.AutoSort = true;
        this._faqEntityDataSource.ConnectionString = this._connectionString;
        this._faqEntityDataSource.DefaultContainerName = "FaqEntities";
        this._faqEntityDataSource.EnableInsert = true;
        this._faqEntityDataSource.EnableUpdate = true;
        this._faqEntityDataSource.EnableDelete = true;
        this._faqEntityDataSource.EntitySetName = "FaqItems";
        this._faqEntityDataSource.EntityTypeFilter = "FaqItem";
        this._faqEntityDataSource.Include = "FaqCategory";
    }

    private void InitializeGrid()
    {
        this.AllowPaging = true;
        this.AllowSorting = true;
        this.AutoGenerateColumns = false;
        this.AutoGenerateEditButton = false;
        this.DataKeyNames = new[]
        {
            "Id"
        };
        this.ShowHeaderWhenEmpty = true;
        this.ShowHeader = true;
        this.ShowFooter = true;
    }

    private void InitializeGridEvents()
    {
        this.RowEditing += this.HandleRowEditing;
        this.RowUpdating += this.HandleRowUpdating;
        this.RowDeleting += this.HandleRowDeleting;
        this.RowCancelingEdit += this.HandleRowCancelingEdit;
    }

    private void InitializeGridColumns()
    {
        this.Columns.Add(new CommandField
        {
            ButtonType = ButtonType.Link,
            ShowDeleteButton = true,
            ShowInsertButton = true,
            ShowEditButton = true,
            ShowCancelButton = true
        });

        this.Columns.Add(new BoundField
        {
            DataField = "CategoryId",
            HeaderText = "CategoryId",
            SortExpression = "CategoryId"
        });

        this.Columns.Add(new BoundField
        {
            DataField = "Question",
            HeaderText = "Question",
            SortExpression = "Question"
        });

        this.Columns.Add(new BoundField
        {
            DataField = "Answer",
            HeaderText = "Answer",
            SortExpression = "Answer"
        });

        this.Columns.Add(new TemplateField
        {
            HeaderText = "Start Date",
            SortExpression = "StartDate",
            ItemTemplate = new DateTimePickerTemplate(ListItemType.Item, "StartDate"),
            EditItemTemplate = new DateTimePickerTemplate(ListItemType.EditItem, "StartDate")
        });

        this.Columns.Add(new TemplateField
        {
            HeaderText = "End Date",
            SortExpression = "EndDate",
            ItemTemplate = new DateTimePickerTemplate(ListItemType.Item, "EndDate"),
            EditItemTemplate = new DateTimePickerTemplate(ListItemType.EditItem, "EndDate")
        });

        this.Columns.Add(new CheckBoxField
        {
            DataField = "IsPublished",
            HeaderText = "Published",
            SortExpression = "IsPublished"
        });
    }

    private void InitializeBinding()
    {
        this.DataSource = this._faqEntityDataSource;

        this.DataBind();
    }

    private void HandleRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        ((GridView)sender).EditIndex = -1;

        this.InitializeBinding();
    }

    private void HandleRowEditing(object sender, GridViewEditEventArgs e)
    {
        ((GridView)sender).EditIndex = e.NewEditIndex;

        this.InitializeBinding();
    }

    private void HandleRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ((GridView)sender).EditIndex = -1;

        this.InitializeBinding();
    }

    private void HandleRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = Rows[e.RowIndex];

        ((GridView)sender).EditIndex = -1;

        this.InitializeBinding();
    }

    protected override void OnInit(EventArgs e)
    {
        if (!this.Page.IsPostBack)
            this.InitializeBinding();
    }
}

}

基本的に、データを取得して表示することはできます。グリッドビューを編集モードに切り替えることもできます。ただし、行の「保存」コマンドを押しても何も起こりません。ページがリロードされ、グリッドビューは編集モードのままになります。(GridView Updating イベントは発生しません!)

コードに明らかなエラーが見られますか?
なにか提案を?

4

0 に答える 0