视图状态、数据表和属性!

发布于 2024-09-12 16:42:24 字数 2547 浏览 9 评论 0原文

我正在开发一个包含两个 RadGrid 的用户控件。当用户选择网格 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();

    }

}

我这样做完全错了吗?有人可以帮忙吗?

提前致谢 希格斯

Im developing a user control that contains two RadGrids. When a user selects a row in grid 1, the page postsback. At that point, I create a Datatable and DataRow and add it to grid 2's datasource.

The problem I am experiencing overall is that the datatable is lost and re-created every time the page is posted back. I therefore tried to save the datatable in the viewstate using a property, but that doesnt seem to have helped. I'm pretty new to using properties full stop, so my code is perhaps wrong.

My solution:

    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();

    }

}

Am i doing this completely wrong? Can anyone help?

Thanks in advance
higgsy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2024-09-19 16:42:24

您是否在调用 Page.DataBind 时未检查 Page.IsPostBack 是否为 true?这将导致第二个网格重新绑定,并且在没有定义的数据源的情况下将为空。

除此之外,定义数据源并将其绑定在 SelectedIndexChanged 中,如果启用了 ViewState,第二个 RadGrid 应保留其数据。

Are you calling Page.DataBind without checking whether or not Page.IsPostBack is true? This would cause the second grid to rebind and, without a defined datasource, will be empty.

Other than that, defining the datasource and binding it in SelectedIndexChanged, if ViewState is enabled, the second RadGrid should persist it's data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文