asp.net ajax&javascript学习笔记(-)
1

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

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml">6
<head runat="server">7
<title>Untitled Page</title> 8

<script type="text/javascript">
9
var dragActive;10
var dragElement;//拖动的div11
var downX,downY;//初始鼠标位置12
var tmp_o_x,tmp_o_y;//初始div位置13
function readyDrag()14


{15
if(event.srcElement.tagName!="DIV")16
return;17
dragActive=1;18
dragElement=event.srcElement.parentNode;19
dragElement.style.position="relative";20
downX=event.clientX;21
downY=event.clientY;22
tmp_o_x=dragElement.style.pixelLeft;23
tmp_o_y=dragElement.style.pixelTop;24
document.onmousemove=startDrag;25
}26
document.onmouseup=endDrag;27
function startDrag()28


{29
if(dragActive==1&&event.button==1&&dragElement!=null)30


{31
dragElement.style.left=tmp_o_x+event.clientX-downX;32
dragElement.style.top=tmp_o_y+event.clientY-downY;33
//dragElement.style.left=event.clientX;34
//dragElement.style.top=event.clientY;35
}36
}37
function endDrag()38


{39
dragActive=0;40
}41
</script>42

<style type="text/css">
43
.newStyle144

{
}{45
font-size:12px;46
overflow: hidden;47
}48
</style>49
</head>50
<body class="newStyle1">51
<form id="form1" runat="server">52
<div> 53
<asp:ScriptManager ID="ScriptManager1" runat="server" 54
EnablePageMethods="True" >55

<scripts >
<asp:ScriptReference Path="PageMethod.js"/></scripts>56
</asp:ScriptManager>57
<br />58
<asp:Label ID="ResultId" runat="server" Text=""></asp:Label>59
<br />60
Key:<asp:TextBox ID="keyText" runat="server"></asp:TextBox>61
62
Value:<asp:TextBox ID="valueText" runat="server"></asp:TextBox>63
<br />64
<asp:Button ID="Button1" runat="server" Text="设置Session" OnClientClick="SetSessionValue('time',Date());return false;"/>65
<asp:Button ID="Button2" runat="server" Text="获取Session" OnClientClick="GetSessionValue('time');return false;"/>66
<input id="Button3" type="button" value="显示信息" onclick="btnNew_onclick();" /></div>67
</form>68
<div id="divMsg" style=" visibility:hidden; border: 2px groove #3366CC;width: 332px;height: 200px;position: relative;z-index: 1000;top: 22px;left: 283px; background-color: #FFFFFF;">69
<div id="dragHandle" onmousedown="readyDrag()" onm style="width: 100%;height: 24px;background-color: #3366CC; cursor:move">70
<span id="divTitle" style="font-family: 'Arial Rounded MT Bold';color: #FFFFFF"></span> <img alt="关闭" src="close.gif" style="position: absolute; right: 2px; cursor:pointer;" onclick="closeDIV();" />71
</div>72
<div id="divContent" style="width: 100%; ">73
</div>74
</div>75
</body>76
</html>77

1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Linq;6
using System.Web;7
using System.Web.Security;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.WebControls.WebParts;11
using System.Web.UI.HtmlControls;12
using System.Xml.Linq;13
using System.Web.Services;14
using System.Web.Script.Services;15
using System.Collections.Specialized;16
[GenerateScriptType(typeof(Employee))]17
public partial class DragTest : System.Web.UI.Page18


{19
protected void Page_Load(object sender, EventArgs e)20

{21

22
}23
[WebMethod]24
public static string GetSessionValue(string key)25

{26
return (string)HttpContext.Current.Session[key];27
}28

29
[WebMethod]30
// Set session state value.31
public static string SetSessionValue(string key, string value)32

{33
HttpContext.Current.Session[key] = value;34
return (string)HttpContext.Current.Session[key];35
}36
[WebMethod]37
public static string[] GetEmplyee()38

{ 39

string[] strs =
{"key","您已打开新选项卡使用选项卡,您可以:使用一个 Internet Explorer 窗口查看所有网页。查看所在页面时打开后台选项卡中的链接。 使用收藏夹和主页选项卡可以一次保存和打开多个网页。若要执行此操作:按住 Ctrl 键单击链接(或者使用鼠标中键)。 使用鼠标中键单击任何选项卡可以关闭该选项卡。 从地址栏或搜索框按 Alt+Enter 可以在新选项卡中打开结果 " };40
return strs;41
}42
}43
public class Employee44


{45
private int m_id;46
public int Id47

{48

get
{ return m_id; }49

set
{ m_id = value; }50
}51
52
private string m_name;53
public string Name54

{55

get
{ return m_name; }56

set
{ m_name = value; }57
}58
59
private string m_email;60
public string Email61

{62

get
{ return m_email; }63

set
{ m_email = value; }64
}65
66
private int m_salary;67
public int Salary68

{69

get
{ return m_salary; }70

set
{ m_salary = value; }71
}72
73
public Employee()74

{75
}76
77
public Employee(int id, string name, string email, int salary)78

{79
m_id = id;80
m_name = name;81
m_email = email;82
m_salary = salary;83
}84
}85

86

1
// PageMethods.js2

3
var displayElement;4

5
// Initializes global variables and session state.6
function pageLoad()7


{8
displayElement = $get("ResultId");9
PageMethods.SetSessionValue("SessionValue", Date(), 10
OnSucceeded, OnFailed);11
}12

13
// Gets the session state value.14
function GetSessionValue(key) 15


{16
PageMethods.GetSessionValue(key, 17
OnSucceeded, OnFailed);18
}19

20
//Sets the session state value.21
function SetSessionValue(key, value) 22


{23
PageMethods.SetSessionValue(key, value, 24
OnSucceeded, OnFailed);25
}26

27
// Callback function invoked on successful 28
// completion of the page method.29
function OnSucceeded(result, userContext, methodName) 30


{31
if (methodName == "GetSessionValue")32

{33
displayElement.innerHTML = "Current session state value: " + 34
result;35
}36
}37

38
// Callback function invoked on failure 39
// of the page method.40
function OnFailed(error, userContext, methodName) 41


{42
if(error !== null) 43

{44
displayElement.innerHTML = "An error occurred: " + 45
error.get_message();46
}47
}48
var div;49
function btnNew_onclick()50


{51
div=document.createElement("DIV");52
div.style.width="100%";53
div.style.height="100%";54
div.style.overflow="hidden";55
div.style.backgroundColor="silver";56
div.style.position = 'absolute';57
div.style.filter="alpha(opacity=70)";58
div.style.left="0";59
div.style.top="0";60
div.style.zIndex="999";61
div.parentNode62
document.body.appendChild(div);63
document.body.style.overflow="hidden";64
PageMethods.GetEmplyee(OnGetSucceeded,OnGetFailed);65
//var em = new Employee();66
}67
function OnGetSucceeded(result)68


{ 69
var str1=result[0];70
var str2=result[1];71
var divTitle=$get("divTitle");72
var divContent=$get("divContent");73
divTitle.innerHTML=str1;74
divContent.innerHTML=str2;75
var divMsg=$get("divMsg");76
divMsg.style.visibility="visible";77
}78
function OnGetFailed(result)79


{ 80
alert('failed');81
}82
function closeDIV(e)83


{ 84
var divMsg=$get("divMsg");85
divMsg.style.visibility="hidden";86
//document.body.removeChild(divMsg);87
document.body.removeChild(div);88
div=null;89
}90
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();91

浙公网安备 33010602011771号