Thomson-Blog ( 学习,学习,再学习;努力,努力,再努力。)
在学习的路上不断成长,成功之路就在脚下。
         简单的模拟Windows文件夹的AJAX无限级菜单树。
         开发环境:VS2005、C#、.net2.0、Access、AJAX引擎是自己写的,没有用到AJAX的DLL。
         运行环境:IE
         具体的功能与界面如下:

         初始菜单:


         展开文件夹:


         新建同级:

         新建子级:

         修改:

         删除:


         右键菜单,模拟Windows失去焦点自动保存,判断是否重名,删除提示!

         下面是3个程序主文件代码(Tree.htm、Tree.aspx、Ajax.js)


 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" >
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=GB2312" /> 
 5     <title>无限级联极菜单树</title>
 6     <script language="javascript" type="text/javascript" src="js/Ajax.js"></script>
 7     <script language="javascript" type="text/javascript">
 8     <!--
 9     function window.onload()
10     {
11         getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
12     }
13     -->
14     </script>
15 </head>
16 <body>
17 <div id='divAjax'></div>
18 <div id="contextmenu" style="text-align:center;border:1px solid #666666;background:#eeeeee;width:60px;padding:5px;display:none;position:absolute"></div>
19 </body>
20 </html>
21 

 


  1 function $(id)
  2 {
  3     return document.getElementById(id);    
  4 }
  5 function echo(obj,html)
  6 {
  7     $(obj).innerHTML=html;
  8 }
  9 function createxmlhttp()
 10 {
 11     var xmlhttp = false;
 12     try    
 13     {
 14           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 15      } 
 16     catch (e) 
 17     {
 18           try 
 19           {
 20                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 21           } 
 22         catch (e) 
 23         {
 24                xmlhttp = false;
 25          }
 26      }
 27     if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
 28     {
 29           xmlhttp = new XMLHttpRequest();
 30         if (xmlhttp.overrideMimeType)
 31         {
 32             //设置MiME类别 
 33             xmlhttp.overrideMimeType('text/xml');
 34         }
 35     }
 36     return xmlhttp;    
 37 }
 38 //向服务器获取数据
 39 function getdata(url,obj,initJS)
 40 {
 41         var xmlhttp = createxmlhttp();
 42         xmlhttp.onreadystatechange=requestdata;
 43         xmlhttp.open("GET",url,true);
 44         xmlhttp.setRequestHeader("If-Modified-Since","0");
 45         xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 46         xmlhttp.send(null);
 47         function requestdata()
 48         {
 49             echo(obj,"<IMG SRC='Images/loading.gif' />&nbsp;<span style=' font-size:12px; color:Black;'>loading..</span><br>");
 50             if(xmlhttp.readyState==4)
 51             {
 52                 if(xmlhttp.status==200)
 53                 {
 54                     echo(obj,xmlhttp.responseText);
 55                     if(initJS != null)
 56                     {
 57                         getInitJS(initJS);
 58                     }
 59                 }
 60             }
 61         }
 62 }
 63 //向服务器发送数据
 64 function postdata(url,obj,data)
 65 {
 66         var xmlhttp= createxmlhttp();
 67         xmlhttp.onreadystatechange=requestdata;
 68         xmlhttp.open("POST", url, true);
 69         xmlhttp.setRequestHeader("If-Modified-Since","0");
 70         xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 71         xmlhttp.send(data);
 72         function requestdata()
 73         {
 74             echo(obj,"<IMG SRC='Images/loading.gif' />&nbsp;<span style=' font-size:12px; color:Black;'>loading..</span><br>");
 75             if(xmlhttp.readyState==4)
 76             {
 77                 if(xmlhttp.status==200)
 78                 {
 79                     if(!postInitJS(xmlhttp.responseText))
 80                     {
 81                         echo(obj,xmlhttp.responseText);
 82                     }
 83                 }
 84             }
 85         }
 86 }
 87 
 88 function ShowSub(id,flag)
 89 {
 90     if(flag=="1")
 91     {
 92         eval("sub" + id).style.display='';
 93         document.images["AddImg" + id].src='Images/subImg.bmp';
 94         return;
 95     }
 96     if(flag=="0")
 97     {
 98         eval("sub" + id).style.display='none';
 99         document.images["AddImg" + id].src='Images/addImg.bmp';
100         return;
101     }
102     if(eval("sub" + id).style.display=='none')
103     {
104         eval("sub" + id).style.display='';
105         document.images["AddImg" + id].src='Images/subImg.bmp';
106         return;
107     }
108     else
109     {
110         eval("sub" + id).style.display='none';
111         document.images["AddImg" + id].src='Images/addImg.bmp';
112         return;
113     }    
114 }
115 
116 function showMenu(id,fatherID)
117 
118     contextmenu.style.posLeft = document.body.scrollLeft + event.x + 10 ;
119     contextmenu.style.posTop = document.body.scrollTop + event.y + 10;
120     var menuHtml = "";
121     menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=getdata('tree.aspx?mode=getAdd&fatherID=" + fatherID + "','add" + fatherID + "','addFocus" + fatherID + "');closeMenu(); >添加同级</span><br />";
122     menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=getdata('tree.aspx?mode=addSub&id=" + id + "','addSub" + id + "','getTree" + id + "');closeMenu(); >添加子级</span><br />";
123     menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=getdata('tree.aspx?mode=getEdit&id=" + id + "','edit" + id + "','editFocus" + id + "');closeMenu(); >修  改</span><br />";
124     menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=closeMenu();if(confirm('你确定要删除此菜单吗?')){postdata('tree.aspx?mode=del&id=" + id + "&fatherID=" + fatherID + "','edit" + id + "');} >删  除</span>";
125     document.getElementById('contextmenu').innerHTML = menuHtml ;
126     contextmenu.style.display = "" ;
127 }
128 
129 function closeMenu()
130 {
131     contextmenu.style.display="none";
132 }
133 
134 document.oncontextmenu=function()
135 {
136     return false;
137 }
138 
139 document.onclick=function()
140 {
141     if(document.activeElement!=contextmenu)
142     {
143         closeMenu();
144     }
145 }
146 function getInitJS(JS)
147 {
148     if(JS.substr(0,6)=='addSub')
149     {
150         id=JS.substr(6,JS.length);
151         getdata('tree.aspx?mode=getAdd&fatherID=+ id,'add' + id,'addFocus' + id);
152     }
153     if(JS.substr(0,8)=='addFocus')
154     {
155         id=JS.substr(8,JS.length);
156         $('addName'+id).select();
157     }
158     if(JS.substr(0,9)=='editFocus')
159     {
160         id=JS.substr(9,JS.length);
161         $('editName'+id).select();
162     }
163     if(JS.substr(0,7)=='getTree')
164     {
165         id=JS.substr(7,JS.length);
166         getdata('tree.aspx?mode=getTree&fatherID=+ id,'sub' + id,'addSub' + id);
167         ShowSub(id,'1');
168     }
169 }
170 function postInitJS(JS)
171 {
172     if(JS.substr(0,5)=='added')
173     {
174         id=JS.substr(5,JS.length);
175         if(id=="0")
176         {
177             getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
178         }
179         else
180         {
181             getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id );
182         }
183         return true;
184     }
185     if(JS.substr(0,6)=='edited')
186     {
187         id=JS.substr(6,JS.length);
188         getdata('tree.aspx?mode=returnEdit&id=+ id,'edit' + id );
189         return true;
190     }
191     if(JS.substr(0,10)=='addNameRep')
192     {
193         id=JS.substr(10,JS.length);
194         alert('此菜单名已经存在');
195         getdata('tree.aspx?mode=getAdd&fatherID=+ id ,'add' + id ,'addFocus' + id);
196         return true;
197     }
198     if(JS.substr(0,11)=='editNameRep')
199     {
200         id=JS.substr(11,JS.length);
201         alert('此菜单名已经存在');
202         getdata('tree.aspx?mode=getEdit&id=+ id ,'edit' + id ,'editFocus' + id);
203         return true;
204     }
205     if(JS.substr(0,10)=='deletError')
206     {
207         id=JS.substr(10,JS.length);
208         alert('请先删除此菜单的子级项');
209         if(id=="0")
210         {
211             getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
212         }
213         else
214         {
215             getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id );
216         }
217         return true;
218     }
219     if(JS.substr(0,7)=='deleted')
220     {
221         id=JS.substr(7,JS.length);
222         if(id=="0")
223         {
224             getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
225         }
226         else
227         {
228             getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id );
229         }
230         return true;
231     }
232     return false;
233 }
234 
235 function postAdd(id)
236 {
237     if($('addName'+id).value=='')
238     {
239         alert('菜单名字不能为空!');
240         $('addName'+id).focus();
241         return false;
242     }
243     data="Name="+($('addName'+id).value);
244     postdata('tree.aspx?mode=saveAdd&fatherID=+ id ,'add' + id,data);
245 }
246 
247 function postEdit(id)
248 {
249     if($('editName'+id).value=='')
250     {
251         alert('菜单名字不能为空!');
252         $('editName'+id).focus();
253         return false;
254     }
255     data="Name="+($('editName'+id).value);
256     postdata('tree.aspx?mode=saveEdit&id=+ id ,'edit' + id,data);
257 }
258 

 


  1 <%@ import namespace="System" %>
  2 <%@ import namespace="System.Data" %>
  3 <%@ import namespace="System.Configuration" %>
  4 <%@ import namespace="System.Collections" %>
  5 <%@ import namespace="System.Configuration" %>
  6 <%@ import namespace="System.Web" %>
  7 <%@ import namespace="System.Web.Security" %>
  8 <%@ import namespace="System.Web.UI" %>
  9 <%@ import namespace="System.Web" %>
 10 <%@ import namespace="System.Web.UI.WebControls" %>
 11 <%@ import namespace="System.Web.UI.WebControls.WebParts" %>
 12 <%@ import namespace="System.Web.UI.HtmlControls" %>
 13 <%@ import namespace="System.Data.OleDb" %>
 14 <%@ Page Language="C#" %>
 15 
 16 <script runat="server">
 17     //Webconfig文件中获取数据库路径
 18     public string g_StrConn = ConfigurationManager.AppSettings["Conn_Access"];
 19     protected void Page_Load(object sender, EventArgs e)
 20     {
 21         string mode = Request.QueryString["mode"];
 22         switch (mode)
 23         {
 24             case "getTree":
 25                 Response.Write(getTree());
 26                 break;
 27             case "getAdd":
 28                 Response.Write(getAdd());
 29                 break;
 30             case "saveAdd":
 31                 Response.Write(saveAdd());
 32                 break;
 33             case "getEdit":
 34                 Response.Write(getEdit());
 35                 break;
 36             case "saveEdit":
 37                 Response.Write(saveEdit());
 38                 break;
 39             case "addSub":
 40                 Response.Write(addSub());
 41                 break;
 42             case "del":
 43                 Response.Write(del());
 44                 break;
 45             case "returnEdit":
 46                 Response.Write(returnEdit());
 47                 break;
 48         }
 49     }
 50     
 51     //******************
 52     //**  获取树结构  **
 53     //******************
 54     public string getTree()
 55     {
 56         string fatherID = Request.QueryString["fatherID"];
 57         if (!IsNumber(fatherID))
 58         {
 59             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
 60         }
 61         string StrSql = "Select * From tree Where FatherID=" + fatherID + " Order By Name Asc";
 62         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
 63         OleDbCommand MyCmd = new OleDbCommand(StrSql, MyConn);
 64         try
 65         {
 66             MyCmd.Connection.Open();
 67             OleDbDataReader Dr = MyCmd.ExecuteReader();
 68             string TopStr = "<a id='add" + fatherID + "'></a><table border='0' cellpadding='0' cellspacing='0' style='width: 100%; height: 10px'>";
 69             string BottomStr = "</table>";
 70             string MiddleStr = "";
 71             while (Dr.Read())
 72             {
 73                 MiddleStr = MiddleStr + "<tr><td align='left' valign='middle' style='width:200px;height:22px;' colspan='2'><span id='addSub" + Dr["ID"].ToString() + "'>";
 74                 string StrSql2 = "Select * From tree Where FatherID=" + Dr["ID"].ToString();
 75                 OleDbConnection MyConn2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
 76                 OleDbCommand MyCmd2 = new OleDbCommand(StrSql2, MyConn2);
 77                 try
 78                 {
 79                     MyCmd2.Connection.Open();
 80                     OleDbDataReader Dr2 = MyCmd2.ExecuteReader();
 81                     if (Dr2.Read())
 82                     {
 83                         MiddleStr = MiddleStr + "<img alt='' id='AddImg" + Dr["ID"].ToString() + "' src='Images/addImg.bmp' onclick=getdata('tree.aspx?mode=getTree&fatherID=" + Dr["ID"].ToString() + "','sub" + Dr["id"].ToString() + "');ShowSub(" + Dr["id"].ToString() + "); />&nbsp;<img alt='' src='Images/folder.bmp' />&nbsp;<a id='edit" + Dr["ID"].ToString() + "'><span style='cursor:hand;font-size:12px; color:Black;' oncontextmenu=showMenu(" + Dr["ID"].ToString() + "," + fatherID + "); onclick=alert('" + Dr["Name"].ToString() + "'); >" + Dr["Name"].ToString() + "</span></a>";
 84                     }
 85                     else
 86                     {
 87                         MiddleStr = MiddleStr + "<span style='font-size:11px; color:Black;'>&nbsp;&nbsp;&nbsp;</span><img alt='' src='Images/folder.bmp' />&nbsp;<a id='edit" + Dr["ID"].ToString() + "'><span style='cursor:hand;font-size:12px; color:Black;' oncontextmenu=showMenu(" + Dr["ID"].ToString() + "," + fatherID + "); onclick=alert('" + Dr["Name"].ToString() + "');>" + Dr["Name"].ToString() + "</span></a>";
 88                     }
 89                     MyCmd2.Connection.Close();
 90                 }
 91                 catch
 92                 {
 93                     return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
 94                 }
 95                 MiddleStr = MiddleStr + "</span></td></tr><tr><td align='left' valign='middle' style='width:22px;'></td><td align='left' valign='middle' ><div style='display:none;' id='sub" + Dr["id"].ToString() + "'></div></td></tr>";
 96             }
 97             MyCmd.Connection.Close();
 98             return TopStr + MiddleStr + BottomStr;
 99         }
100         catch
101         {
102             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
103         }
104     }
105 
106     //************************
107     //**  获取节点新增表单  **
108     //************************
109     public string getAdd()
110     {
111         string addHtml = "";
112         string fatherID = Request.QueryString["fatherID"];
113         if (!IsNumber(fatherID))
114         {
115             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
116         }
117         addHtml = addHtml + "<span style='font-size:11px; color:Black;'>&nbsp;&nbsp;&nbsp;</span><img alt='' src='Images/folder.bmp' />&nbsp;<input onblur=postAdd(" + fatherID + ") name='addName" + fatherID + "' type='text' value='新建菜单项' size='15'><br />";
118         return addHtml;
119     }
120     
121     //**********************
122     //**  保存新增的节点  **
123     //**********************
124     public string saveAdd()
125     {
126         string id = Request.QueryString["id"];
127         string Name = Request["Name"];
128         if (Name.CompareTo(""== 0)
129         {
130             return "nameNull";
131         }
132         string fatherID = Request["fatherID"];
133         if (!IsNumber(fatherID))
134         {
135             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
136         }
137         if (!checkName(Name,""))
138         {
139             return "addNameRep" + fatherID;
140         }
141         string StrSql = "Select * From tree";
142         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
143         try
144         {
145             MyConn.Open();
146             OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
147             OleDbCommandBuilder oCB = new OleDbCommandBuilder(odAdapt);
148             DataSet DS = new DataSet();
149             odAdapt.Fill(DS);
150             DataRow DR = DS.Tables[0].NewRow();
151             DR["name"= Name;
152             DR["fatherID"= fatherID;
153             DS.Tables[0].Rows.Add(DR);
154             odAdapt.Update(DS);
155             MyConn.Close();
156             return "added" + fatherID;
157         }
158         catch
159         {
160             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
161         }
162     }
163 
164     //********************
165     //**  新增子级节点  **
166     //********************
167     public string addSub()
168     {
169         string changeHtml = "";
170         string id = Request.QueryString["id"];
171         if (!IsNumber(id))
172         {
173             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
174         }
175         string StrSql = "Select * From tree Where id=" + id;
176         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
177         try
178         {
179             MyConn.Open();
180             OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
181             DataSet DS = new DataSet();
182             odAdapt.Fill(DS);
183             DataTable DT = DS.Tables[0];
184             changeHtml = changeHtml + "<img alt='' id='AddImg" + DT.Rows[0]["ID"].ToString() + "' src='Images/addImg.bmp' onclick=getdata('tree.aspx?mode=getTree&fatherID=" + DT.Rows[0]["ID"].ToString() + "','sub" + DT.Rows[0]["ID"].ToString() + "');ShowSub(" + DT.Rows[0]["ID"].ToString() + "); />&nbsp;<img alt='' src='Images/folder.bmp' />&nbsp;<a id='edit" + DT.Rows[0]["ID"].ToString() + "'><span style='cursor:hand;font-size:12px; color:Black;' oncontextmenu=showMenu(" + DT.Rows[0]["ID"].ToString() + "," + DT.Rows[0]["fatherID"].ToString() + "); onclick=alert('" + DT.Rows[0]["name"].ToString() + "'); >" + DT.Rows[0]["name"].ToString() + "</span></a>";
185             MyConn.Close();
186         }
187         catch
188         {
189             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
190         }
191         return changeHtml;
192     }
193     
194     //************************
195     //**  获取节点修改表单  **
196     //************************
197     public string getEdit()
198     {
199         string editHtml = "";
200         string startValue = "";
201         string id = Request.QueryString["id"];
202         if (!IsNumber(id))
203         {
204             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
205         }
206         string StrSql = "Select * From tree Where id=" + id;
207         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
208         try
209         {
210             MyConn.Open();
211             OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
212             DataSet DS = new DataSet();
213             odAdapt.Fill(DS);
214             DataTable DT = DS.Tables[0];
215             startValue = DT.Rows[0]["name"].ToString().Trim();
216             MyConn.Close();
217             editHtml = editHtml + "<input onblur=postEdit(" + id + ") name='editName" + id + "' type='text' value='" + startValue + "' size='15'>";
218             return editHtml;
219         }
220         catch
221         {
222             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
223         }
224     }
225     
226     //**********************
227     //**  保存修改的节点  **
228     //**********************
229     public string saveEdit()
230     {
231         string id = Request.QueryString["id"];
232         if (!IsNumber(id))
233         {
234             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
235         }
236         string Name = Request["Name"];
237         if (Name.CompareTo("")==0)
238         {
239             return "nameNull";
240         }
241         if (!checkName(Name,id))
242         {
243             return "editNameRep" + id;
244         }
245         string StrSql = "Select * From tree Where id=" + id;
246         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
247         try
248         {
249             MyConn.Open();
250             OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
251             OleDbCommandBuilder oCB = new OleDbCommandBuilder(odAdapt);
252             DataSet DS = new DataSet();
253             odAdapt.Fill(DS);
254             DataTable DT = DS.Tables[0];
255             DT.Rows[0]["name"= Name;
256             odAdapt.Update(DS);
257             MyConn.Close();
258             return "edited" + id;
259         }
260         catch
261         {
262             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
263         }
264     }
265     
266     //************************
267     //**  返回修改后的节点  **
268     //************************
269     public string returnEdit()
270     {
271         string editHtml = "";
272         string startValue = "";
273         string fatherID = "";
274         string id = Request.QueryString["id"];
275         if (!IsNumber(id))
276         {
277             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
278         }
279         string StrSql = "Select * From tree Where id=" + id;
280         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
281         try
282         {
283             MyConn.Open();
284             OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
285             DataSet DS = new DataSet();
286             odAdapt.Fill(DS);
287             DataTable DT = DS.Tables[0];
288             startValue = DT.Rows[0]["name"].ToString().Trim();
289             fatherID = DT.Rows[0]["fatherID"].ToString().Trim();
290             MyConn.Close();
291             editHtml = editHtml + "<span style='cursor:hand;font-size:12px; color:Black;' oncontextmenu=showMenu(" + id + "," + fatherID + "); onclick=alert('" + startValue + "'); >" + startValue + "</span>";
292             return editHtml;
293         }
294         catch
295         {
296             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
297         }
298     }
299 
300     //****************
301     //**  删除节点  **
302     //****************
303     public string del()
304     {
305         string id = Request.QueryString["id"];
306         if (!IsNumber(id))
307         {
308             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
309         }
310         string fatherID = Request.QueryString["fatherID"];
311         if (!IsNumber(fatherID))
312         {
313             return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据!</span>";
314         }
315         string StrSql = "select * From Tree where fatherID=" + id;
316         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
317         OleDbCommand MyCmd = new OleDbCommand(StrSql, MyConn);
318         try
319         {
320             MyCmd.Connection.Open();
321             OleDbDataReader Dr = MyCmd.ExecuteReader();
322             if (Dr.Read())
323             {
324                 return "deletError" + fatherID;
325             }
326             MyCmd.Connection.Close();
327         }
328         catch
329         {
330             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
331         }
332         string StrSql2 = "Delete * From Tree where id=" + id;
333         OleDbConnection MyConn2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
334         OleDbCommand myCmd2 = new OleDbCommand(StrSql2, MyConn2);
335         try
336         {
337             MyConn2.Open();
338             myCmd2.ExecuteReader();
339             MyConn2.Close();
340             return "deleted" + fatherID;
341         }
342         catch
343         {
344             return "<span style='font-size:12px; color:Black;'>数据库连接失败!</span>";
345         }
346     }
347     
348     //************************
349     //**  判断名字是否存在  **
350     //************************
351     public bool checkName(string name,string id)
352     {
353         name = MoveBidStr(name);
354         string StrSql = "";
355         if (id.CompareTo(""== 0)
356         {
357             StrSql = "select * from Tree where name='" + name + "'";
358         }
359         else
360         {
361             StrSql = "select * from Tree where name='" + name + "' and id<>" + id;
362         }
363         OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
364         OleDbCommand MyCmd = new OleDbCommand(StrSql, MyConn);
365         try
366         {
367             MyCmd.Connection.Open();
368             OleDbDataReader Dr = MyCmd.ExecuteReader();
369             if (Dr.Read())
370             {
371                 return false;
372             }
373             else
374             {
375                 return true;
376             }
377             MyCmd.Connection.Close();
378         }
379         catch
380         {
381             return false;
382         }
383     }
384     //**************************
385     //**  判断是否是数字组合  **
386     //**************************
387     public bool IsNumber(string strDate)
388     {
389         if (strDate == null)
390         {
391             return false;
392         }
393         if (strDate.Equals(string.Empty))
394         {
395             return false;
396         }
397 
398         Regex numRegex = new Regex(@"0*[0-9][0-9]*$");
399         return numRegex.IsMatch(strDate);
400     }
401     //******************
402     //**  过滤'"符号  **
403     //******************
404     public string MoveBidStr(string strString)
405     {
406         string str;
407         str = strString;
408         if (str != null)
409         {
410             str = str.Replace("'""''");
411             str = str.Replace(((char)39).ToString(), ((char)39 + (char)39).ToString());
412         }
413         return str;
414     }
415 </script>
416 

 

posted on 2009-03-18 11:25  Thomson-Blog  阅读(333)  评论(0编辑  收藏  举报