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