视图状态、数据表和属性!
我正在开发一个包含两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在调用
Page.DataBind
时未检查Page.IsPostBack
是否为 true?这将导致第二个网格重新绑定,并且在没有定义的数据源的情况下将为空。除此之外,定义数据源并将其绑定在
SelectedIndexChanged
中,如果启用了 ViewState,第二个 RadGrid 应保留其数据。Are you calling
Page.DataBind
without checking whether or notPage.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.