慕容星空小站

.NET技术研究进行中....
posts - 12, comments - 127, trackbacks - 4, articles - 0

2009年5月14日

老婆睡了,随手写点代码,貌似明天要用到。

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProcessTable.aspx.cs" Inherits="ProcessTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <link href="App_Themes/Main/Main.css" rel="stylesheet" type="text/css" />

    <script src="JavaScript/jquery-1[1].2.6-vsdoc.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
        function DeleteCurrentRow(obj) {
            $("#gridTable a").click(function() {
                if ($(this).attr("id") == obj.id) {
                    if (confirm("are you sure to delete this one?")) {
                        $(this).parent().parent().remove();
                    }
                }
            });
        }
        function EditCurrentRow(obj) {
            $("#gridTable a").click(function() {
                if ($(this).attr("id") == obj.id) {
                    $(this).parent().parent().find("td").not(":last").each(function(i) {
                        var html = $(this).html();
                        $(this).html("<input type='text' value='" + html + "' /><input type='hidden' value='" + html + "' />");
                    });
                    var random = new Date().getTime();
                    $(this).parent().html("<a href='#' id='update" + random + "' onclick='UpdateRow(this)'>update</a>&nbsp;<a href='#' id='cancel" + random + "' onclick='CancelRow(this)'>cancel</a>");
                }
            });
        }

        //Update
        function UpdateRow(obj) {
            $("#gridTable a").click(function() {
                if ($(this).attr("id") == obj.id) {
                    $(this).parent().parent().find("td").not(":last").each(function(i) {
                        var html = $(this).find("input[@type=text]").val();
                        $(this).html(html);
                    });
                    var random = new Date().getTime();
                    $(this).parent().html("<a href=# id='update" + random + "' onclick='EditRow(this)'>edit</a>&nbsp;<a href=# id='delete" + random + "' onclick='DeleteRow(this)'>delete</a>");
                    $("#newID").show();
                }
            });
        }
        //Cancel
        function CancelRow(obj) {
            $("#gridTable a").click(function() {
                if ($(this).attr("id") == obj.id) {
                    $(this).parent().parent().find("td").not(":last").each(function(i) {
                        var html = $(this).find("input[@type=hidden]").val();
                        $(this).html(html);
                    });
                    var random = new Date().getTime();
                    $(this).parent().html("<a href=# id='update" + random + "' onclick='EditRow(this)'>edit</a>&nbsp;<a href=# id='delete" + random + "' onclick='DeleteRow(this)'>delete</a>");
                }
            });
        }
        //Edit
        function EditRow(obj) {
            $("#gridTable a").click(function() {
                if ($(this).attr("id") == obj.id) {

                    $(this).parent().parent().find("td").not(":last").each(function(i) {
                        var html = $(this).html();
                        $(this).html("<input type='text' value='" + html + "' /><input type='hidden' value='" + html + "' />");
                    });
                    var random = new Date().getTime();
                    $(this).hide();
                    $(this).parent().html("<a href='#' id='update" + random + "' onclick='UpdateRow(this)'>update</a>&nbsp;<a href='#' id='cancel" + random + "' onclick='CancelRow(this)'>cancel</a>");
                }

            });
        }
        //Delete
        function DeleteRow(obj) {
            $("#gridTable a").click(function() {
                if ($(this).attr("id") == obj.id) {
                    if (confirm("are you sure to delete this one?")) {
                        $(this).parent().parent().remove();
                    }
                }
            });
        }

        //Insert
        function InsertRow() {
            var html = "<tr>";
            $("#gridTable tr:last").find("td").not(":last").each(function(i) {
                html += "<td>";
                html += "<input type='text' />";
                html += "</td>";
            });
            var random = new Date().getTime();
            html += "<td><a href=# id='update" + random + "' onclick='UpdateRow(this)'>update</a>&nbsp;<a href=# onclick='CancelNew()'>cancel</a></td>";
            html += "</tr>";
            $("#gridTable").html($("#gridTable").html() + html);
            $("#newID").hide();
            event.returnValue = false;
        }
        function CancelNew() {
            $("#gridTable").find("tr:last").remove();
            $("#newID").show();
        }

        function Submit() {
            if ($("#gridTable").find("input").length) {
                alert('can\' submit!');
                return false;
            }
            $("#gridTable tr").each(function(i) {
                $(this).find("td").not(":last").each(function(j) {
                    alert($(this).html());
                });
            });

        }
       
 
   
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="height: 30px">
        </div>
        <asp:GridView ID="gridTable" runat="server" Width="500px" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField ItemStyle-Width="20%" HeaderText="姓名">
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="65%" HeaderText="地址">
                    <ItemTemplate>
                        <%# Eval("Address") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="#" id="edit" onclick="EditCurrentRow(this)">edit</a> <a href="#" id="delete"
                            onclick="DeleteCurrentRow(this)">delete</a>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <a href="#" id="newID" onclick="InsertRow()">Add New</a>
        <br />
        <a href="#" id="submit" onclick="Submit()">Submit</a>
    </div>
    </form>
</body>
</html>

posted @ 2009-05-14 00:44 ustbwuyi 阅读(441) | 评论 (0)编辑

2007年5月28日

     摘要: 最近因为项目需要,研究了一下邮件的发送和接收,发现现在这方面的问题很多.虽然网上这方面的资料很多,但是真正应用起来仍然会发现不少问题,而且很多人都抱怨书上或者网上的一些代码应用起来是失败的,并且针对附件方面的处理不太完善,针对这种情况我把最近研究所得做一个小小的总结,希望能对大家有所帮助。 首先来谈谈发送邮件的问题。 发送邮件现在应用得最多的两种情况就是利用.NET自带的发送邮件和利用jmail组...  阅读全文

posted @ 2007-05-28 15:29 ustbwuyi 阅读(4558) | 评论 (22)编辑

2007年4月12日

     摘要: 泛型1、 什么是泛型? 泛型允许你在编译时间实现类型安全。它们允许你创建一个数据结构而不限于特定的数据类型。即通过参数化类型来实现在同一份代码上操作多种数据类型。泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用。之所以我们称它为泛型,是因为我们在定义的时候,只为它所包含的对象指派了一个类型占位符,而不是将它定义为确定的类型,只有在创建该集合的实例时,才会给它指派一个...  阅读全文

posted @ 2007-04-12 17:43 ustbwuyi 阅读(498) | 评论 (1)编辑

2007年3月19日

 

     最近老总提了一个小功能,在搜索网吧列表的时候加上网吧所属代理商这个条件,原有的搜索条件是一个地区二级联动,现在需要根据不同的地区显示不同的代理商集合。
即在触发地区下拉框的onchange事件时,代理商的下拉框选项也相应的改变,比如选择地区 湖南—〉长沙,那么代理商下拉框只显示长沙的代理商。
    本来认为这个很好实现,但实际改起来的时候发现问题多多,主要问题是原有的地区联动是用js实现的,它的数据源是一个xml文件,当然如果下拉框是服务器端控件那么问题是很好解决的,现在是html控件一下子似乎还真有些不好改,想了几种办法实现起来都不理想,最后将思路转向用ajax来实现问题才迎刃而解,现在仔细一想,像这种情况似乎只有用ajax才能比较好的解决,如果是在地区下拉框的onchange事件里向后台进行一次提交,将地区下拉框的id传过去的话,实际上产生的回发会将地区联动下拉框重新初始化。
    现在我具体谈谈这个ajax实现的过程。
    首先页面当然需要定义一个下拉框的html控件。

 <select id="Agent" name="Agent"></select>

接下来当然是定义XmlHttpRequest对象。
   

var xmlhttp;
function CreateXmlHttp()
   
{  


   
//非IE浏览器创建XmlHttpRequest对象
    if(window.XmlHttpRequest)
    
{
     xmlhttp
=new XmlHttpRequest();
    }

    
//IE浏览器创建XmlHttpRequest对象
     if(window.ActiveXObject)
    
{
    
try
    
{
     xmlhttp
=new ActiveXObject("Microsoft.XMLHTTP"); 
    }

    
catch(e)
    
{
    
try{
     xmlhttp
=new ActiveXObject("msxml2.XMLHTTP");
     }

     
catch(ex){}
    }

    }

   }
  

这个在我的多篇blog文章里都有阐述,就不多说了。

接下来当然是利用该对象来发送条件,获得数据,并且将获得的数据绑定到agent这个下拉框。
在地区下拉框的onchange事件里面触发函数AjaxSend();

function AjaxSend()
{
     
//创建XmlHttpRequest对象
     CreateXmlHttp();     
     
if(!xmlhttp)
     
{
         alert(
"创建xmlhttpRequest发生异常!");
         
return false;
     }
   
     
//获取地区下拉框的value值,作为条件发送
     var ss=document.getElementById("a2").value.substring(0,4);
     }

    
//要发送的url,UserAjax我专门用来取数据
     url="UserAjax.aspx?area="+ss;
     xmlhttp.open(
"POST",url,false); 
        
   
     xmlhttp.onreadystatechange
=function()
     

 
         
if(xmlhttp.readyState==4)
         
{
             
if(xmlhttp.status==200)
             
{  
               
//清空原下拉框
              document.getElementById("agent").options.length=0;           
               
//str为返回的一个字符串,形式为"0001/代理商1,0002/代理商2,0003/代理商3"
               var str=xmlhttp.responseText;
              
//将该字符串分割为数组形式
               var strs=str.split(",");
               document.getElementById(
"agent").options.add(new Option("----------","000000"));
               
for(var i=0;i<strs.length-1;i++)
               
{
                
//获取value值(编号)
                var a=strs[i].substring(0,strs[i].lastIndexOf("/"));
                
//获取绑定内容
                var b=strs[i].substring(strs[i].lastIndexOf("/")+1,strs.length);
                
//绑定到下拉框
                document.getElementById("agent").options.add(new Option(b,a));               
               }

                 
             }
 
          }
 
      }
           
      xmlhttp.send();
}

另外顺便介绍一下UserAjax接收到该地区编号后获取数据返回字符串的过程。

 string Area = Request.QueryString["area"].ToString();
        DataTable data 
= "生成DataTable,涉及到公司核心代码,省略"
        
string aa = "";
        
for (int i = 0; i < data.Rows.Count; i++)
        
{
            
if (aa == "")
            
{
                aa 
= data.Rows[i]["id"].ToString()+"/"+data.Rows[i]["name"].ToString();
            }

            
else
            
{
                aa 
= aa + "," + data.Rows[i]["id"].ToString() +"/"+ data.Rows[i]["name"].ToString();
            }

        }

       Response.Write(aa);

这样,一个比较棘手的问题用ajax就获得了完美解决,并且不会因向后台回发而导致下拉框初始化,导致选项改变,亲爱的朋友,看了这个例子,你对ajax是不是也有了
更好的认识呢?

 

posted @ 2007-03-19 11:26 ustbwuyi 阅读(2309) | 评论 (7)编辑

2007年2月8日

     摘要: 在写这篇文章之前,曾经写过一篇关于AJAX技术的随笔,不过涉及到的方面很窄,对AJAX技术的背景、原理、优缺点等各个方面都很少涉及null。这次写这篇文章的背景是因为公司需要对内部程序员做一个培训。项目经理找到了我,并且征询我培训的主题,考虑到之前Javascript、CSS等WEB开发技术都已经讲解过了,所以决定针对AJAX这一块做一个比较系统的培训,所以这篇文章实际上是一个培训的材料。 在这篇...  阅读全文

posted @ 2007-02-08 16:31 ustbwuyi 阅读(4729) | 评论 (55)编辑

2007年1月30日

     摘要: 1 、文件上传最近做广告发布系统,不可避免的遇到上传图片文件的问题。基本原理是将图片上传到服务器,然后将它的路径保存在数据库,显示的时候从服务器取出该路径,让img直接指向这个路径即可。当然在上传的时候需要判断文件类型,比如是jpg、gif,swf等。在这里我是用正则表达式来验证的,该方法非常有效,而直接用endwith方法来判断有时候会出现一些莫名其妙的问题。//获取上传文件的路径stringf...  阅读全文

posted @ 2007-01-30 17:25 ustbwuyi 阅读(430) | 评论 (1)编辑

2006年10月23日

     摘要: 在javascript中,event事件是一个必不可少的讨论话题,它在和用户的交互中起到了很重要的作用。今天我们就来讨论一下JavaScript中的事件处理,并且结合它来阐叙Ajax框架实现拖动效果的原理了。一、 Event对象 1Event对象的主要属性和方法  event代表事件的状态,专门负责对事件的处理,它的属性和方法能帮助我们完成很多和用户交互的操作,下面我们就来看看它的一些属性和方法。...  阅读全文

posted @ 2006-10-23 15:22 ustbwuyi 阅读(630) | 评论 (0)编辑

2006年9月28日

     摘要: 关于ajax,是最近炒得非常火的一种技术,并且时下它也是非常流行。当然,它并不是什么新技术,而是在各种已有的技术和支持机制下的一个统一。在我的项目中,偶尔也会用到ajax,用来给用户一些无刷新的体验。用过几次之后,我个人决定对它的原理和运行机制做一个总结。 ajax这个名字据说是Asynchronous JavaScript + XML的简写,实际上,它由下列几种技术组合而成。 1.使用CSS和X...  阅读全文

posted @ 2006-09-28 14:42 ustbwuyi 阅读(1046) | 评论 (2)编辑

2006年8月24日

     摘要: 自从W3C建立了DOM标准 (W3C DOM)之后,以及DOM和浏览器兼容之后,DOM在实际应用中越来越广泛。 DOM是DocumentObject Model的简称,中文名称为文档对象模型。它的主要作用是建立网页与 Script 或程序语言沟通的桥梁。 我们平时可操作及建立文件的属性、方法及事件都以并且用“对象”来展现(例如,document 就代表“文件本身...  阅读全文

posted @ 2006-08-24 11:06 ustbwuyi 阅读(1571) | 评论 (0)编辑

2006年8月15日

     摘要: C#.Net 常用函数和方法集 1、DateTime数字型System.DateTimecurrentTime=newSystem.DateTime();1.1取当前年月日时分秒currentTime=System.DateTime.Now;1.2取当前年int年=currentTime.Year;1.3取当前月int月=currentTime.Month;1.4取当前日int日=currentT...  阅读全文

posted @ 2006-08-15 11:31 ustbwuyi 阅读(277) | 评论 (0)编辑