悍匪

回忆过去,展望未来......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JavaScript无刷新操作(数据库记录的加载删改)

Posted on 2008-06-07 09:05  悍匪  阅读(661)  评论(0)    收藏  举报
        比较无聊,都是用脚本划的table,似乎效率也不怎么好。
        下面的代码是整个用户控件的HTML页面:
        几处 oHttpReq.open("POST""Ajax.aspx?TypeID=1&RequestID=" + RequestID, false);Ajax.aspx页面就是操作数据库页面。
        
        
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContentFilesList.ascx.cs" Inherits="Content_Contorls_ContentFilesList" %>
<script>
var GetStatus
=0;//当前问题状态
var sumpage=1;//总页数
var cuurpage=1;//当前页
var pagecount=null;//总记录数量

//删除表格
function   _delAllRows(tbobj)   
  
{   
      var   obj
=document.getElementById(tbobj);   
      
if   (obj==null||obj.rows==null)   return   false;   
      var   len
=obj.rows.length;   
      
if   (len==null||len<1)   return   false;   
      
for   (var   i=0;   i<len;   i++)   
          obj.deleteRow(
0);   
      
return   true;   
  }
   

function LoadDataList()
{
    _delAllRows(
"question");
    var oHttpReq 
= new ActiveXObject("MSXML2.XMLHTTP"); 
    var oDoc 
= new ActiveXObject("MSXML2.DOMDocument"); 
    
    var RequestID 
= getargs();
    
    oHttpReq.open(
"POST""Ajax.aspx?TypeID=1&RequestID=" + RequestID, false); 
    oHttpReq.send(
""); 
    result 
= oHttpReq.responseText; 
    
    
if(oHttpReq.readyState==4)
    
{
        eval(
"loading.style.display=\"none\";");
    }

    
    a 
= new Array(2);
    a
=result.split('|爩|');
    oDoc.loadXML(a[
0]);//返回的DataSet数据集
    var localurl=a[1];//总记录条数    
    
//alert(localurl);
    itemsID = oDoc.selectNodes("//Table/ID"); 
    itemsNewName 
= oDoc.selectNodes("//Table/NewName"); 
    itemsOldName 
= oDoc.selectNodes("//Table/OldName"); 
    itemsState 
= oDoc.selectNodes("//Table/State"); 

    var itemsLength
=itemsID.length; 
  
for(i=0;i<itemsLength;i++//将小类的类名和编号赋予
  {   //创建表格
      newRow=question.insertRow(i);
        newRow.bgColor
="#ffffff";
          
for (j=0; j < 4; j++
               newCell
=newRow.insertCell(j);
       }
 
            
           var thisEname 
= itemsOldName[i].text;
           
           var fileType
="";
           
if(thisEname.split('.').length>0)
                fileType
=thisEname.split('.')[1];
                
           
switch(fileType.toLowerCase())
           
{
                
//类型
                case "doc" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/fileDoc.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
                
case "ppt" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/filePpt.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
                
case "jpg" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/fileJpg.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
                
case "gif" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/fileGif.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
                
case "txt" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/fileTxt.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
                
case "rar" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/buttonTheme.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
                
default:case "rar" : document.all.question.rows(i).cells(0).innerHTML = "<img src=\"../Resource/Images/Default/Icon/fileNone.gif\"  style=\"border: solid 0 #000;width:16px;height:16px\" />"break;
           }


           document.all.question.rows(i).cells(
0).width=16;
           document.all.question.rows(i).cells(
0).align="left";
           
           
//*
           
//获取路径
           var FileUrl='';
           var url
=unescape(window.location.href);//获取URL,unescape解析URL字符 
           
//var GetfileLinkLength = fileLink.IndexOf("UpLoadFiles");
           var GetFilesLength = url.indexOf("Content");
           
//alert(GetFilesLength);
           var fileurl = url.substring(0, GetFilesLength) + "UpLoadFiles/" + itemsNewName[i].text;
           
//alert(fileurl);
            
//名称
           localurl = localurl + '\UpLoadFiles';
           localurl 
= localurl + itemsNewName[i].text;
           
/*
            if(ReportFileStatus(localurl))   
                 alert(1);   
            else     
                 alert(0);    
           
*/
 
           document.all.question.rows(i).cells(
1).innerHTML =
            
"<div title="+itemsOldName[i].text+" style=\"overflow:hidden; text-align:left; text-overflow:ellipsis; white-space:nowrap; PADDING-TOP: 1px; PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; width:150px;\">"                                       
            
+"<a href='"+fileurl+"' style=\"cursor:hand;\" target=\"_blank\" >"+itemsOldName[i].text+" </a>"
            
+"</div>";

           document.all.question.rows(i).cells(
1).width=150;
           document.all.question.rows(i).cells(
1).align="left";
           
//*/
           var StateText = itemsState[i].text;
           
switch(StateText)
           
{
                
//类型
                case "0" : document.all.question.rows(i).cells(2).innerHTML = "<a title='当前状态:未审核' style=\"cursor:hand;\" onclick=\"tracestate("+itemsID[i].text+",1,'审核','"+itemsOldName[i].text+"')\"><font color=black>×</a></font>"break;
                
case "1" : document.all.question.rows(i).cells(2).innerHTML = "<a title='当前状态:已开通' style=\"cursor:hand;\" onclick=\"tracestate("+itemsID[i].text+",2,'关闭','"+itemsOldName[i].text+"')\"><font color=green>√</a></font>"break;
                
case "2" : document.all.question.rows(i).cells(2).innerHTML = "<a title='当前状态:已关闭' style=\"cursor:hand;\" onclick=\"tracestate("+itemsID[i].text+",1,'开通','"+itemsOldName[i].text+"')\"><font color=red>⊙</a></font>"break;
                
default :  document.all.question.rows(i).cells(2).innerHTML = "<a title='当前状态:未知' style=\"cursor:hand;\" onclick=\"tracestate("+itemsID[i].text+",2,'关闭','"+itemsOldName[i].text+"')\"><font color=blue>?</a></font>"break;
           }

           document.all.question.rows(i).cells(
2).width=40;
           document.all.question.rows(i).cells(
2).align="left";
          
           document.all.question.rows(i).cells(
3).innerHTML = "<a style=\"cursor:hand;\" class=\"ApageLink\" onclick=\"tracedelete("+itemsID[i].text+",'"+itemsOldName[i].text+"')\">删除</a>";
           document.all.question.rows(i).cells(
3).width=50;
           document.all.question.rows(i).cells(
3).align="center";
  }

    
}

 function  ReportFileStatus(filespec)   
 
{   
    var   fso,   s   
=   filespec;   
    fso   
=   new   ActiveXObject("Scripting.FileSystemObject");   
    
if   (fso.FileExists(filespec))   
          
return true;   
    
else     
          
return false
 }

  
function tracestate(ID,StateID,strTxt,strName)
{
    
if(window.confirm('您确认' + strTxt + strName +  ''))
    
{
        var StateReq 
= new ActiveXObject("MSXML2.XMLHTTP");     
        StateReq.open(
"POST""Ajax.aspx?TypeID=2&RequestID="+ ID + "&State=" + StateID, false);
        StateReq.send(
""); 
        Sresult 
= StateReq.responseText;

        
if(Sresult=="1")
        
{
            LoadDataList();
        }

        
else
        
{
            alert(strTxt 
+ '失败,请稍后再试');
            
return;
        }

    }

}


function tracedelete(DeleteID,oldName)
{
    
if(window.confirm('您确认删除附件:'+oldName+''))
    
{
        var DelReq 
= new ActiveXObject("MSXML2.XMLHTTP");     
        DelReq.open(
"POST""Ajax.aspx?TypeID=3&RequestID="+ DeleteID, false);
        DelReq.send(
""); 
        Dresult 
= DelReq.responseText;

        
if(Dresult=="1")
        
{
            LoadDataList();
            var allowCount 
= document.getElementById("hidFileAllowCount").value;
            alert(allowCount);
            allowCount
++;
            alert(allowCount);
            document.getElementById(
"hidFileAllowCount").value = allowCount;
            document.getElementById(
'attach').innerText = "继续添加附件";
        }

        
else
        
{
            alert(
'删除失败,请稍后再试');
            
return;
        }

    }

}


function getargs()

   var url
=unescape(window.location.href);//获取URL,unescape解析URL字符
   var allargs=url.split("?")[1];//获取?后的值
   var args=allargs.split("&");//获取参数数组
   for(var i=0;i<args.length; i++)
   
{
     
if(args[i].split("=")[0]=="ID")
     
     
return args[i].split("=")[1];
     
break;
   }

}


window.onload 
= function(){LoadDataList();}  
</script>
    
<table border="0" align="left" cellpadding="0" cellspacing="0" width="60%">        
            
<tr style="height:auto; width:98%" align="center">
            
<td align="left">
                
<table align="center" border="0" cellpadding="0" cellspacing="0" style="width: 100%">
                    
<tr><td>
                        
<div id="loading">
                        
<img src="../Resource/Images/Default/loading.gif" alt="数据加载中,请稍后"  />&nbsp;</div>
                    
</td></tr>
                    
<tr>
                        
<td>
                            
<table id="question" align="center" border="0" cellpadding="0" cellspacing="0" width="98%">
                                
<tr>
                                    
<td style="height: auto">
                                    
</td>
                                
</tr>
                            
</table>
                        
</td>
                    
</tr>
                
</table>
               
</td></tr>
    
</table>

        Ajax.cs 代码
        1、返回数据集:
private void GetFileList(string RequestID)
    
{
        
if (string.IsNullOrEmpty(RequestID))
        
{
            DataSet ds 
= CommonSqlExec.ExecReturnDataSet("SELECT * FROM Tables WHERE [ID]=" + RequestID, 1);
            
string localurl = Request.MapPath("~/");
            
//回写DataSet为XML,客户端接收
            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            writer.Formatting 
= Formatting.Indented;
            writer.Indentation 
= 4;
            writer.IndentChar 
= ' ';
            writer.WriteStartDocument();
            ds.WriteXml(writer);
            writer.Flush();
            Response.Write(
"|爩|" + localurl);//个人需要加的,可以不要的
            Response.End();
            writer.Close();
        }

    }
        这个就是加载数据的HTML页面:LoadDataList()方法。。。
        
        2、更改状态
private void StateFile(string RequestID, string State)
    
{
        
bool b = CommonSqlExec.Exec("UPDATE Tables SET State=" + State + " WHERE [ID]=" + RequestID, 1);
        
if (b)
        
{
            Response.Write(
"1");
            Response.End();
        }

        
else
        
{
            Response.Write(
"0");
            Response.End();
        }

    }
        
        就这么简单。。。