2010-08-05 85 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應該保留其數據。