为何还是无法解决呢?我给出原码,大家帮帮忙吧 -- 控件代码如下 -- using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Drawing; using System.Data; using System.Xml; using System.Runtime; using System.Runtime.Serialization;namespace WebControlLibrary { /// <summary> /// DataGridExp 的摘要说明。 /// </summary>
[SerializableAttribute] public class DataGridExt : System.Web.UI.WebControls.DataGrid { public DataGridExt() { this.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(DataGridExp_ItemCreated); this.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(DataGridExp_ItemDataBound); this.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(DataGridExp_PageIndexChanged); this.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(DataGridExp_SortCommand); Width = Unit.Percentage(98);
如:
DataView dv = ds.Tables[0].defaultView;
你可以试试用new的dataset来转换成DV。
是否必须是在指定DataSource时就必须指定为DataView才可以呢,如:DataSet1.Tables[0].DefaultView?
可是,有个问题,为了实现分页,我在空间中DataSource是用ViewSate保存的。这样在设置DataSource = DataSet1.Tables[0].DefaultView 时会出现如下错误:类型“System.Data.DataView”必须标记为 Serializable 或者具有 ReferenceConverter 以外的 TypeConverter,才能被放置在视图状态中。
如何解决呢?若DataSource不用ViewState保存,如何在控件中实现分页功能呢?
关于这一问题,你可以在初始化DG的时候就把DG是datasource放在session中,最好是完整的dataset,那么当你排序的时候,直接把session中的dataset付值给DG就可以了!再帮定即可。
在class前面加
[SerializableAttribute]编写自定义控件里可以用session,Context.Session["hehe"]
confused
-- 控件代码如下 --
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Xml;
using System.Runtime;
using System.Runtime.Serialization;namespace WebControlLibrary
{
/// <summary>
/// DataGridExp 的摘要说明。
/// </summary>
[SerializableAttribute]
public class DataGridExt : System.Web.UI.WebControls.DataGrid
{
public DataGridExt()
{
this.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(DataGridExp_ItemCreated);
this.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(DataGridExp_ItemDataBound);
this.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(DataGridExp_PageIndexChanged);
this.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(DataGridExp_SortCommand); Width = Unit.Percentage(98);
BackColor = Color.Aquamarine;
AlternatingItemStyle.BackColor = Color.Azure;
AlternatingItemStyle.ForeColor = Color.Black;
AlternatingItemStyle.HorizontalAlign = HorizontalAlign.Center;
EditItemStyle.BackColor = Color.Cornsilk;
EditItemStyle.ForeColor = Color.DarkTurquoise; FooterStyle.BackColor = Color.Firebrick;
FooterStyle.ForeColor = Color.AliceBlue;
FooterStyle.HorizontalAlign = HorizontalAlign.Center;
HeaderStyle.BackColor = Color.Ivory;
HeaderStyle.ForeColor = Color.Goldenrod;
HeaderStyle.HorizontalAlign = HorizontalAlign.Center; ItemStyle.BackColor = Color.IndianRed;
ItemStyle.ForeColor = Color.LightGray;
ItemStyle.HorizontalAlign = HorizontalAlign.Center; PagerStyle.BackColor = Color.DimGray;
PagerStyle.ForeColor = Color.YellowGreen;
PagerStyle.HorizontalAlign = HorizontalAlign.Center;
PagerStyle.NextPageText = "下一页";
PagerStyle.PrevPageText = "上一页"; SelectedItemStyle.BackColor = Color.Blue;
SelectedItemStyle.ForeColor = Color.NavajoWhite;
} public override object DataSource
{
get
{
return Context.Session["DataSource"];
//return ViewState["DataSource"];
}
set
{
base.DataSource = value;
Context.Session["DataSource"]=value;
//ViewState["DataSource"]=value;// _DataSource = value;
}
}
public bool SortAscending
{
get
{
object o = ViewState["SortAscending"];
if (o == null) return true;
return (bool)o;
}
set
{
ViewState["SortAscending"] = value;
}
} private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
} public void DataGridExp_ItemCreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer && Items.Count == 0)
{
int j=e.Item.Cells.Count;
for (int i=j-1; i>0; i--) e.Item.Cells.RemoveAt(i);
e.Item.Cells[0].ColumnSpan = j;
e.Item.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Item.Cells[0].Text = "当前数据表格中没有数据";
}
} public void DataGridExp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex < 0) return;
e.Item.Attributes.Add("style","CURSOR:hand");
this.Page.RegisterStartupScript("TempColor","<script>var TempColor;</script>");
e.Item.Attributes.Add("onMouseOver", "TempColor=this.style.backgroundColor;this.style.backgroundColor='DarkKhaki';");
e.Item.Attributes.Add("onMouseOut", "this.style.backgroundColor=TempColor;");
ButtonColumn a = new ButtonColumn();
if (e.Item.Cells[0].GetType().Equals(a))
e.Item.Attributes.Add("OnClick",Page.GetPostBackClientEvent(e.Item.Cells[0].Controls[0],String.Empty));
} public void DataGridExp_PageIndexChanged(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
CurrentPageIndex = e.NewPageIndex;
DataBind();
} public void DataGridExp_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
SortAscending = !SortAscending;
DataView dv;
try
{
dv = (DataView)DataSource;
dv.Sort = e.SortExpression.ToString();
dv.Sort+= (SortAscending)?" ASC": " Desc";
DataSource = dv;
DataBind();
}
catch (Exception err)
{
try
{
dv = ((DataSet)DataSource).Tables[0].DefaultView;
dv.Sort = e.SortExpression.ToString();
dv.Sort+= (SortAscending)?" ASC": " Desc";
DataSource = dv;
DataBind();
}
finally{}
}
finally{}
}
public override void DataBind()
{
base.DataBind();
if (Items.Count==0)
{
PagerStyle.Visible = false;
ShowFooter = true;
}
}
}
}
-- 代码结束 --上面的代码实现了DataGrid的排序,分页,无数据时显示提示等功能,使用很简单,在程序里随便指定个数据源(dataset),绑定一下就可以了。(有个bug,不能在设计页面里指定数据源)。
这个控件现在的状态是在设计页面显示“创建自定义控件出错”,但是运行起来是正确的。原因在DataSource属性中用了Session(同上贴说明),将其改为ViewState(就是被我注视掉的部分)后排序功能无法正常使用。
我的要求不高,能在控件内实现分页和排序就像现在这样,但在设计页面里正常就可以了。希望高手帮忙,最好能出原码。多谢多谢