using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace Weltop.WebControl
{
/// <summary>
/// WebDataGrid 自定义DataGrid
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebDataGrid runat=server ></{0}:WebDataGrid>")]
public class WebDataGrid :System.Web.UI.WebControls.DataGrid
{
private string text;
private string m_SelectedFieldValue=string.Empty ;
private string m_CssClassRowSelected=string.Empty ;
private string m_CssClassRowMouseMove=string.Empty ;
private string m_ScriptPath=string.Empty ;
private bool m_ClientSelectMode;
private bool m_MorePageSeleced;
private bool m_DefaultSet;
private string m_ClientSelectedValue=string.Empty ;
private string m_CheckAllText=string.Empty ;
private TemplateColumn tm;
private string SelectValueList=string.Empty ;
public WebDataGrid()
{
m_DefaultSet=true;
//this.ItemCreated += new DataGridItemEventHandler( MyDataGrid_ItemCreated );
}
// private void MyDataGrid_ItemCreated( object sender , System.Web.UI.WebControls.DataGridItemEventArgs e )
// {
//// if ( e.Item.ItemType == ListItemType.Pager )
//// {
//// Literal msg = new Literal();
//// msg.Text = "把这三句代码替换成呈现实际翻页控件外观的代码。";
//// ( ( TableCell ) e.Item.Controls[0] ).Controls.Add( msg );
//// }
//
// }
public TemplateColumn CheckColumn
{
get
{
return tm;
}
set
{
tm=value;
}
}
/// <summary>
/// 客户端选择的值
/// </summary>
public string ClientSelectedValue
{
get
{
return m_ClientSelectedValue;
}
set
{
m_ClientSelectedValue=value;
}
}
[Description("客户端多选的时候,是否记录上次选择的值,通常用于多页选择。"),Bindable(true),
Category("Behavior"),
DefaultValue(false)]
public bool MorePageSeleced
{
get
{
return m_MorePageSeleced;
}
set
{
m_MorePageSeleced=value;
}
}
[Description("客户端选择的方式,False=单选,True=多选"),Bindable(true),
Category("Behavior"),
DefaultValue(false),
DesignOnly(true)]
public bool ClientSelectMode
{
get
{
return m_ClientSelectMode;
}
set
{
if(m_ClientSelectMode && value==false)
{
this.Columns.Remove(tm);
tm=null;
}
m_ClientSelectMode=value;
if(m_ClientSelectMode && tm==null)
{
tm=new TemplateColumn();
tm.ItemTemplate=new ColumnTemplate();
ColumnTemplate2 tmHead=new ColumnTemplate2 ();
tmHead.CheckAllText ="全选";
tm.HeaderTemplate =tmHead;
this.Columns.AddAt(0,tm);
}
}
}
[Description("客户端选择的脚本文件地址"),Bindable(true),
Category("Behavior"),
DefaultValue("")]
public string ScriptPath
{
get
{
return m_ScriptPath;
}
set
{
m_ScriptPath=value;
}
}
[Description("用于选择一行时的用户CSS类名"),Bindable(true),
Category("Appearance"),
DefaultValue("USeleced")]
public string CssClassRowSelected
{
get
{
return m_CssClassRowSelected;
}
set
{
m_CssClassRowSelected=value;
}
}
[Description("用于鼠标悬浮在一行时的用户CSS类名"),Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string CssClassRowMouseMove
{
get
{
return m_CssClassRowMouseMove;
}
set
{
m_CssClassRowMouseMove=value;
}
}
[Description("每一行选择的值,对应于某一个列"),Bindable(true),
Category("Data"),
DefaultValue("")]
public string SelectedFieldValue
{
get
{
return m_SelectedFieldValue;
}
set
{
m_SelectedFieldValue = value;
}
}
[Description("该属性用于表示控件显示的文字内容"),Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
[Description("多选风格时候全选复选框的文字"),Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string CheckAllText
{
get
{
return m_CheckAllText;
}
set
{
m_CheckAllText = value;
if(tm!=null)
{
ColumnTemplate2 tmHead=(ColumnTemplate2)tm.HeaderTemplate ;
tmHead.CheckAllText =value;
}
}
}
[Description("是否应用默认的样式和脚本设置"),Bindable(true),
Category("Appearance"),
DefaultValue(true)]
public bool DefaultSet
{
get
{
return m_DefaultSet;
}
set
{
m_DefaultSet = value;
if(!m_DefaultSet)//清除默认设置
{
this.CssClass ="";
this.AlternatingItemStyle .CssClass ="";
this.ItemStyle .CssClass ="";
this.HeaderStyle.CssClass ="";
this.CssClassRowMouseMove ="";
this.CssClassRowSelected ="";
this.ScriptPath ="";
}
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
//output.Write(Text);
if(m_DefaultSet)
{
//默认样式名
if(this.CssClass =="")
this.CssClass ="dg_table";
if(this.AlternatingItemStyle.CssClass =="")
this.AlternatingItemStyle .CssClass ="dg_alter";
if(this.ItemStyle.CssClass =="" )
this.ItemStyle .CssClass ="dg_item";
if(this.HeaderStyle.CssClass =="" )
this.HeaderStyle.CssClass ="dg_header";
if(this.CssClassRowMouseMove =="")
this.CssClassRowMouseMove ="Umove";
if(this.CssClassRowSelected =="")
this.CssClassRowSelected ="Uselected";
if(this.ScriptPath =="")
this.ScriptPath ="singleTableRow.js";
}
base.Render (output);
if(m_ClientSelectMode)
{
string script="<script language=\"javascript\">\n "+
"<!--\n";
script+="SetCheckValues();\n";
if(m_MorePageSeleced)//允许多页选择
script+="InitLastSelected('"+m_ClientSelectedValue+"');\n";
script+="//-->\n</script>\n";
output.Write(script);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
if(m_DefaultSet)
{
//自定义属性必须在控件运行初始化时候设置默认值
if(this.CssClassRowMouseMove =="")
this.CssClassRowMouseMove ="Umove";
if(this.CssClassRowSelected =="")
this.CssClassRowSelected ="Uselected";
if(this.ScriptPath =="")
this.ScriptPath ="singleTableRow.js";
}
if(this.CheckAllText=="")
this.CheckAllText ="全选";
if(m_ClientSelectMode)
{
text="多选状态";//this.Columns[0].GetType().ToString ();
this.Columns.RemoveAt (1);//运行时删除空白模板列
}
else
{
m_MorePageSeleced=false; //分页选择仅在多选模式有效
text="单选状态";
}
}
/// <summary>
/// 预呈现处理,主要处理多页选择记录问题
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
if(m_ClientSelectMode)
{
string OldSelectValueList=this.Page.Request.Form["SelectValueList"];
if(SelectValueList!=string.Empty )//如果经过了数据绑定
SelectValueList=SelectValueList.Remove (SelectValueList.Length -1,1);
else
SelectValueList=OldSelectValueList;
this.Page.RegisterHiddenField("SelectValueList",SelectValueList);//记录复选框当前的值
string CurrentSelectedValue=this.Page.Request.Form["CID"];//当前选择的值
string SHValue=this.Page.Request.Form["SHValue"];//客户端发生选择事件的标记
//多页选择处理
if(m_MorePageSeleced)
{
string LastSelectedValues=this.Page.Request.Form["LastSelectedValues"];
if(CurrentSelectedValue!=null && SHValue=="-1")//客户端发生了选择事件
{
if( LastSelectedValues!="")
{
//如果 LastSelectedValues 中的项 在OldSelectValueList 中存在,则删除相关项。
LastSelectedValues=DeleRepStringList(LastSelectedValues,OldSelectValueList);
if(LastSelectedValues!="")
{
LastSelectedValues=LastSelectedValues.Replace (",,","");
m_ClientSelectedValue=CurrentSelectedValue+","+LastSelectedValues;
}
else
m_ClientSelectedValue=CurrentSelectedValue;
}
else
m_ClientSelectedValue=CurrentSelectedValue;
}
else
{
if( LastSelectedValues!="" && SHValue=="-1")
{
//如果 LastSelectedValues 中的项 在OldSelectValueList 中存在,则删除相关项。
LastSelectedValues=DeleRepStringList(LastSelectedValues,OldSelectValueList);
if(LastSelectedValues!="")
{
LastSelectedValues=LastSelectedValues.Replace (",,","");
m_ClientSelectedValue=LastSelectedValues;
}
else
m_ClientSelectedValue="";
}
else
m_ClientSelectedValue=LastSelectedValues;
}
this.Page.RegisterHiddenField("LastSelectedValues",m_ClientSelectedValue);
}
else
{
m_ClientSelectedValue=CurrentSelectedValue;
}
}
}
/// <summary>
/// 去除字符串中的重复项
/// </summary>
/// <param name="ObjStr">目标字符串列表,形如 “1,2,3”</param>
/// <param name="SourceStr">源字符串列表,形如 “1,2,3”</param>
/// <returns>返回目标串</returns>
private string DeleRepStringList(string ObjStr,string SourceStr)
{
string limit=",";
string strTemp=string.Empty ;
string[] arrLSV=ObjStr.Split (limit.ToCharArray ());
SourceStr+=",";
ObjStr+=",";
for(int i=0;i<arrLSV.Length ;i++)
{
strTemp=arrLSV[i]+",";
if(strTemp!="," && SourceStr.IndexOf (strTemp)!=-1)
ObjStr=ObjStr.Replace (strTemp,"");
}
return ObjStr;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
this.Page.RegisterHiddenField("SHValue","");//注册单选值控件
//string SingleScriptPath="singleTableRow.js";
//注册选择样式脚本
string script= "<script language=\"javascript\" src=\""+m_ScriptPath+"\" type=\"text/Jscript\"></script>\n "+
"<script language=\"javascript\">\n "+
"<!--\n"+
"cssRowSelected=\""+m_CssClassRowSelected+"\";\n"+
"cssRowMouseMove=\""+CssClassRowMouseMove+"\";\n"+
"//-->\n" +
"</script>";
this.Page.RegisterClientScriptBlock ("ClientSelect",script);
}
protected override void OnItemDataBound (DataGridItemEventArgs e)
{
base.OnItemDataBound (e);
if(e.Item.ItemType ==ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem || e.Item.ItemType ==ListItemType.SelectedItem)
{
string argstring = "this,'"+m_SelectedFieldValue+"'";//在实例控件绑定,例如 DataBinder.Eval(e.Item.DataItem, "ProductID").ToString();
e.Item.Attributes.Add ("onclick","myclick("+argstring+")");
e.Item.Attributes.Add ("onmouseover","mymove(this)");
e.Item.Attributes.Add ("onmouseout","myout(this)");
if(m_ClientSelectMode)
{
LiteralControl cb=(LiteralControl)e.Item.Cells[0].Controls [0];// .FindControl ("CID");
if(cb!=null)
{
cb.Text = "<input type='checkbox' name='CID' value='"+m_SelectedFieldValue+"'>" +(e.Item.ItemIndex+1).ToString ();
SelectValueList+=m_SelectedFieldValue+",";//复选框选择值列表,在没有数据绑定的时候备用
}
}
}
}
protected override void OnItemCreated(DataGridItemEventArgs e)
{
base.OnItemCreated (e);
if(e.Item.ItemType ==ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem || e.Item.ItemType ==ListItemType.SelectedItem )
{
if(m_ClientSelectMode)
{
LiteralControl cb=(LiteralControl)e.Item.Cells[0].Controls [0];// .FindControl ("CID");
if(cb!=null)
{
//绑定序号
cb.Text = "<input type='checkbox' name='CID' >" +(e.Item.ItemIndex+1).ToString ();
}
}
}
}
}
/// ColumnTemplate 从ITemplate继承。
/// "InstantiateIn"定义子控件的属于谁
/// <summary>
/// 多选模版列
/// </summary>
public class ColumnTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
LiteralControl l1=new LiteralControl ("<input type='checkbox' name='CID'>");
container.Controls.Add(l1);
}
}
/// <summary>
/// 全选模版列
/// </summary>
public class ColumnTemplate2 : ITemplate
{
private string _text="" ;
public string CheckAllText
{
set{_text=value;}
get{return _text;}
}
public void InstantiateIn(Control container)
{
LiteralControl l1=new LiteralControl ("<input type=\"checkbox\" name=\"CheckAll\" value=\"ON\" onclick=\"CheckedAll()\">"+_text);
container.Controls.Add(l1);
}
}
}
上面只是部分源码。需详细源码请联系本人!注明:求pwmis分页组件源码