2010-08-05 86 views
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(); 

    } 

} 

我这样做完全错了吗?谁能帮忙?

在此先感谢 higgsy

+0

当你说'我的解决方案'时,你的意思是这段代码片段有效吗? – Rabid 2010-08-05 17:53:37

回答

1

你打电话Page.DataBind不检查Page.IsPostBack是否是真的?这将导致第二个网格重新绑定,并且在没有定义的数据源的情况下将为空。

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