0

2 つの RadGrids を含むユーザー コントロールを開発しています。ユーザーがグリッド 1 の行を選択すると、ページはポストバックします。その時点で、Datatable と DataRow を作成し、グリッド 2 のデータソースに追加します。

私が全体的に経験している問題は、ページがポストバックされるたびにデータテーブルが失われ、再作成されることです。したがって、プロパティを使用してビューステートにデータテーブルを保存しようとしましたが、それは役に立たなかったようです。プロパティを完全に使用するのは初めてなので、コードが間違っている可能性があります。

私の解決策:

    public class DynamicDocumentSelectorWebUITypeEditor : Telerik.Cms.Web.UI.WebUITypeEditor<string>
    {

    private System.Data.DataTable _oDataTable;

public System.Data.DataTable getTable() {

        System.Data.DataTable oDataTable = new System.Data.DataTable();
        oDataTable.Columns.Add(new System.Data.DataColumn("DocumentID", typeof(string)));
        oDataTable.Columns.Add(new System.Data.DataColumn("DocumentName", typeof(string)));
        oDataTable.Columns.Add(new System.Data.DataColumn("DocumentExtension", typeof(string)));

        return oDataTable;

    }

public System.Data.DataTable oDataTable {
        get {
            object o = this.ViewState["DataTable"];
            if(o == null) {
                return _oDataTable;
            }
            return (System.Data.DataTable)o;
        }
        set {
            this._oDataTable = value;
            this.ViewState["DataTable"] = value;
        }
    }

    protected override void CreateChildControls() {
        base.CreateChildControls();

        if (this.oDataTable == null) {

            this.oDataTable = getTable();

        }

        }

//the following function is executed when a row in grid 1 is selected
    protected void GridDocumentsInLibrary_SelectedIndexChanged(object sender, EventArgs e) {

        //loop through each selected row
        foreach (Telerik.Web.UI.GridDataItem oItem in GridDocumentsInLibrary.SelectedItems) {

            //System.Data.DataTable oDt = this.oDataTable;

            foreach (System.Data.DataRow oDataRow in this.oDataTable.Rows) {

                //check whether the row already exists in the datatable
                //if (oDataRow["DocumentID"] != oItem["DocumentID"].Text) {

                    System.Data.DataRow dr = this.oDataTable.NewRow();
                    dr["DocumentID"] = oItem["DocumentID"].Text;
                    dr["DocumentName"] = oItem["DocumentName"].Text;
                    dr["DocumentExtension"] = oItem["DocumentExtension"].Text;
                    this.oDataTable.Rows.Add(dr);

                //}

            }

        }

        //set datasource of second grid
        GridSelectedDocuments.DataSource = this.oDataTable;
        GridSelectedDocuments.DataBind();

    }

}

私はこれを完全に間違っていますか?誰でも助けることができますか?

よろしくお願いします

4

1 に答える 1

1

true かPage.DataBindどうかを確認せずに呼び出していますか? Page.IsPostBackこれにより、2 番目のグリッドが再バインドされ、データソースが定義されていないと空になります。

それ以外では、データソースを定義して でバインドしSelectedIndexChanged、ViewState が有効になっている場合、2 番目の RadGrid はそのデータを保持する必要があります。

于 2010-08-05T17:55:16.597 に答える