关于asp.net DataGrid 重新封装代码?
本人想封装DataGrid自定义属性,但是当用到AutoGenerateColumns该属性时,数据就无法出现,不知道哪里漏写了什么代码,请各位大侠帮忙。 --------------------编程问答-------------------- 描述太少了,可以详细一些,加上些代码什么的? --------------------编程问答-------------------- 不好意思,代码如下:public class HVDDataGrid : DataGrid, INamingContainer
{
private System.Web.UI.WebControls.DataGrid datagrid = new DataGrid();
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
LiteralControl l = new LiteralControl("");
l.RenderControl(writer);
for (int ii = 0; ii < thecel().Length; ii++)
{
BoundColumn tm = new BoundColumn();
tm.HeaderText = thecel()[ii];
tm.DataField = thecel()[ii];
datagrid.Columns.Add(tm);
DataBind();
}
datagrid.RenderControl(writer);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
public override void DataBind()
{
datagrid.DataBind();
}
public override object DataSource
{
get
{
return this.datagrid.DataSource;
}
set
{
this.datagrid.DataSource = value;
}
}
public override DataGridColumnCollection Columns
{
get
{
return base.Columns;
}
}
public override bool AutoGenerateColumns
{
get
{
return datagrid.AutoGenerateColumns;
}
set
{
datagrid.AutoGenerateColumns = value;
}
}
public string[] thecel()
{
return cells.Split(',');
}
private string _cells="";
public String cells
{
get { return _cells; }
set { _cells = value; }
}
} --------------------编程问答--------------------
private string _cells="";
public String cells
{
get { return _cells; }
set { _cells = value; }
}
===============
public String cells
{
get { return ViewState["cells"]!=null?ViewState["cells"]:String.Empty; }
set { ViewState["cells"]= value; }
}
==========
自定义属性建议用视图状态,不然你的属性状态怎么保存?
--------------------编程问答-------------------- 还有就是楼主既然继承了DataGrid,为什么还要实例化一个DataGrid?
private System.Web.UI.WebControls.DataGrid datagrid = new DataGrid();
--------------------编程问答--------------------
复合控件的做法,应该是重载CreateChildControls,然后在里面进行实例化,然后设置ChildControlsCreated 属性为true避免重复生成控件,提高性能,使用的时候,先用EnsureChildControls方法检测控件是否生成
=====================
--------------------编程问答-------------------- 设计时支持错误,连智能提示都没了。 --------------------编程问答-------------------- 楼主,我给你改好了,设计时支持也有了,AutoGenerateColumns的属性的错误也解决了,记得给分^_^
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
{
public class HVDDataGrid : DataGrid, INamingContainer
{
private System.Web.UI.WebControls.DataGrid datagrid ;
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();//使用控件前,先检测控件是否已经生成,避免“未将对象设置引用对象的实例”的错误
AddAttributesToRender(writer);
LiteralControl l = new LiteralControl("");
l.RenderControl(writer);
for (int ii = 0; ii < thecel().Length; ii++)
{
BoundColumn tm = new BoundColumn();
tm.HeaderText = thecel()[ii];
tm.DataField = thecel()[ii];
datagrid.Columns.Add(tm);
DataBind();
}
datagrid.RenderControl(writer);
}
protected override void CreateChildControls()
{
datagrid = new DataGrid();
this.Controls.Add(datagrid);
base.CreateChildControls();
ChildControlsCreated = true;//避免重复生成控件
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
public override void DataBind()
{
datagrid.DataBind();
}
public override object DataSource
{
get
{
return this.datagrid.DataSource;
}
set
{
this.datagrid.DataSource = value;
}
}
public override DataGridColumnCollection Columns
{
get
{
return base.Columns;
}
}
public override bool AutoGenerateColumns
{
get
{
return datagrid.AutoGenerateColumns;
}
set
{
datagrid.AutoGenerateColumns = value;
}
}
public string[] thecel()
{
return cells.Split(',');
}
public String cells
{
get { return ViewState["cells"] != null ? (string)ViewState["cells"] : String.Empty; }
set { ViewState["cells"] = value; }
}
}
}
=============
--------------------编程问答-------------------- 是啊,现在数据都出不来了。 --------------------编程问答-------------------- 使用示例:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
{
public class HVDDataGrid : DataGrid, INamingContainer
{
private System.Web.UI.WebControls.DataGrid datagrid ;
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();//使用控件前,先检测控件是否已经生成,避免“未将对象设置引用对象的实例”的错误
AddAttributesToRender(writer);
LiteralControl l = new LiteralControl("");
l.RenderControl(writer);
for (int ii = 0; ii < thecel().Length; ii++)
{
BoundColumn tm = new BoundColumn();
tm.HeaderText = thecel()[ii];
tm.DataField = thecel()[ii];
datagrid.Columns.Add(tm);
DataBind();
}
datagrid.RenderControl(writer);
}
protected override void CreateChildControls()
{
datagrid = new DataGrid();
this.Controls.Add(datagrid);
ChildControlsCreated = true;//避免重复生成控件
base.CreateChildControls();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
public override void DataBind()
{
datagrid.DataBind();
}
public override object DataSource
{
get
{
return this.datagrid.DataSource;
}
set
{
this.datagrid.DataSource = value;
}
}
public override DataGridColumnCollection Columns
{
get
{
return base.Columns;
}
}
public override bool AutoGenerateColumns
{
get
{
EnsureChildControls();
return datagrid.AutoGenerateColumns;
}
set
{
EnsureChildControls();
datagrid.AutoGenerateColumns = value;
}
}
public string[] thecel()
{
return cells.Split(',');
}
public String cells
{
get { return ViewState["cells"] != null ? (string)ViewState["cells"] : String.Empty; }
set { ViewState["cells"] = value; }
}
}
}
<FW:HVDDataGrid ID="HVDDataGrid1" runat="server" AutoGenerateColumns="false" cells="UserID,UserName">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"UserID") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"UserID") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</FW:HVDDataGrid>
--------------------编程问答-------------------- 呵呵呵,谢谢了,但是,在使用该控件时,加上 this.HVDDataGrid1.AutoGenerateColumns = false;数据还是出不来了。 --------------------编程问答--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("UserID", Type.GetType("System.Int32"));
DataColumn dc2 = new DataColumn("UserName", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["UserID"] = 1;
dr["UserName"] = "user" + i;
dt.Rows.Add(dr);
}
HVDDataGrid1.DataSource = dt;
HVDDataGrid1.DataBind();
}
}
补充:.NET技术 , ASP.NET