javascript代码
<script>
var curObj;
var shell=1;
function sortTable(L) {
var start=new Date()
var i;
var theRows=new Array();
for(i=1;i<DataGrid1.rows.length;i++) {
theRows[i-1]=new Array(DataGrid1.rows[i].cells[L].innerText.toLowerCase(),DataGrid1.rows[i].outerHTML);
//alert(DataGrid1.rows[i].cells[L].innerText.toLowerCase());
//alert(DataGrid1.rows[i].outerHTML);
}
if (shell==1){
shell=2;
//window.SRC1.innerHTML=DataGrid1.rows[0].cells[3].innerText.toLowerCase()+' <img src="../images/down.gif" >'
switch(parseInt(L)){
//case 7:
// theRows.sort(sortRows1);
// break;
case 4,5:
theRows.sort(sortRows2);
break;
default:
theRows.sort(sortRows);
}
}
else{
shell=1;
//window.SRC1.innerHTML=DataGrid1.rows[0].cells[3].innerText.toLowerCase()+' <img src="../images/up.gif">'
switch(parseInt(L)){
//case 7:
// theRows.sort(sortRows11);
// break;
case 4,5:
theRows.sort(sortRows22);
break;
default:
theRows.sort(sortRows0);
}
}
var str=''
for(i=0;i<theRows.length;i++) {
str+=theRows[i][1];
}
DataGrid1.outerHTML='<table border="0" cellPadding="-1" cellSpacing="0" BorderColor="White" width="100%" bordercolor="#ffffff" name="DataGrid1" id="DataGrid1">'+DataGrid1.rows[0].outerHTML+str+'</table>'
curObj=null;
//alert(new Date()-start)
return ;
}
//降序-------------------
function sortRows(x,y) {
if(x[0]>y[0]) return -1;
else if(x[0]<y[0]) return 1;
else return 0;
}
function sortRows1(x,y) {
if(parseInt(x[0])>parseInt(y[0])) return -1;
else if(parseInt(x[0])<parseInt(y[0])) return 1;
else return 0;
}
function sortRows2(x,y) {
aa=ReplaceAll(x[0],"/","-")
bb=ReplaceAll(y[0],"/","-")
if(aa>bb) return -1;
else if(aa<bb) return 1;
else return 0;
}
//end 降序---------------------
//升序------------------ -
function sortRows0(x,y) {
if(x[0]<y[0]) return -1;
else if(x[0]>y[0]) return 1;
else return 0;
}
function sortRows11(x,y) {
if(parseInt(x[0])<parseInt(y[0])) return -1;
else if(parseInt(x[0])>parseInt(y[0])) return 1;
else return 0;
}
function sortRows22(x,y) {
aa=ReplaceAll(x[0],"/","-")
bb=ReplaceAll(y[0],"/","-")
if(aa<bb) return -1;
else if(aa>bb) return 1;
else return 0;
}
//end 升序---------------------
function ReplaceAll(s,s1,s2){
//把s种的s1全部替换成s2
var i;
s=""+s;
i=s.indexOf(s1);
if(i!=-1)
return s.substr(0,i)+s2+ReplaceAll(s.substr(i+s1.length,s.length-i-s1.length),s1,s2);
else
return s;
}
</script>
<script language="javascript">
function slt_all()
{
for(var i=2;i<=document.getElementById('DataGrid1').rows.length;i++)
{
document.getElementById('DataGrid1__ctl'+i+'_chkDel').checked = document.getElementById('chkAll').checked;
}
}
</script>
后台代码
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Header)
{
for(int i=0;i<e.Item.Cells.Count;i++)
{
e.Item.Cells[i].Attributes["onClick"]="javascript:sortTable("+i+")";
}
}
}