English 中文(简体)
Viewstate, datatable and properties!
原标题:

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

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签