-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathASP.net and jQuery=Erxin.txt.bak
164 lines (154 loc) · 4.51 KB
/
ASP.net and jQuery=Erxin.txt.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
ASP.net and jQuery=Erxin
:jQuery Overview
>$, the jQuery function
>selectors
>events
>chaining
>effects
>DOM manipulation
>AJAX
:jQuery history
>open source project, http://ejohn.org, start in 2005
>current website, http://www.jquery.com
>full support by microsoft after vs 2008
:asp.net using jquery
>download jquery-version.min.js for client side
>download jquery-version.vsdoc.js for intelligent spelling
>run function when document is ready
- pageLoad(){}, javascript function for asp.net automatic invoke after page loaded
- alternative way is use $(function(){}); this Anonymous function will be called when the document is ready.
>load js libray
- use asp.net script manager
- use html styple reference style, only need to drag and drop the library into the page
>drag the wanted version of jquery library to the *.js file to reference the library in asp.net
>$ function usage
- pass in css string to do a css selector
- pass html string to manipulate, such as append to body
- DOM element to bind events or set properties
- function object(invoked on document ready)
:selectors
>how to find element
- base on css syntax
- find by id, class, element name and hierarchical position
:select pseudo-classes
>furthure refinement of selection
- always start with colon, :
- examples include :link, :visited, :odd 奇数, :even 偶数, :not, :first
:visited, the element is visited by client
:working with attributes
>attr(), function
- reads a property value from first match
- set one or more properties on all matched elements
>class function
- addclass()
- removeclass()
- toggleClass
:chaining
> jquery use a build pattern and it can call continues
$(function(){
$("a").mouseover(function(){$(this).addClass("highlight");})
.mouseout(function(){$(this).removeClass("highlight");})
.filter(":even").addClass("evenrow").end()
.filter(":odd").addClass("oddrow").end()
.filter(":contains('sub_string')").attr("href", "attribute_value");
});
the function end() will reverst all the destructive filter to the original
:effects
>basics
hide, show, toggle
>sliding
slideUp, slideDown, slideToggle
>fading
fadeIn, fadeOut, fadeTo
>custom animate function
- $("section").hide();
- $("#menu > p").click(function(){$(this).next().slideToggle("slow")});
:Server communication
>high level options
- load(), load html from xmlHttp request to DOM element
- get()
- post()
- getJSON()
- ex.
$("a").click(getInstructionInfo);
function getInstructionInfo(event){
var instructorName = $(this).text();
$("#detail").load(URL, parameters);
return true;
}
>low level options
- ajax
>using get() and post()
- $.post(URL,
paramters,
callback(result){},
return_data_type_string
);
return data type could be JSON, html, text
>ajax error
$.ajax(
{
type:"POST",
url:"getInstructureInfo.ashx",
data:{"instructor":instructorName},
timeout:5000, //ms
success:function(result){},
error:function(xhr, status, exception)
{
$("#detail").html
(
"There was a error.<br/>" +
"Status:" + status + "<br/>" +
"XHR Status:" + xhr.statusText + "<br/>"
);
}
}
);
>global ajax events
$("#id").ajaxComplete(function(event, xhr, options){...})
.ajaxError
.ajaxSend
.ajaxStop
.ajaxSuccess
>call wcf services
- directly use asp.net ajax library to call wcf services
- use mvc pattern and use xmlhttprequest to call the wcf services
* requires wcf services enable webHttpBinding
* the webservice will return JSON format data, if the data include datetime fields, it will get error as microsoft use different formate for datetime data.
* use low level ajax method
$.ajax(
{
type:"POST",
url:url_to_wcf_file/wcf_method_string,
data:JSON_format_parameters,
timeout:5000,
contentType:"application/json",
dataFilter:parseJson, //use regex replace the datetime format data
success:onSuccess
}
);
function parseJson(data, type)
{
var dateRegEx = new RegExp('(^|[^\\\\])...');//copy from microsoft ajax library to convert datetime
var exp = data.replace(dateRegEx, "$1new Date($2)");
var result = eval('('+exp+')');
return result.d;
}
function onSuccess(wcf_return_data)
{
...
}
:jquery use could custom ui widget from jquery.com
:jquery plugins
>extend jquery
jquery.log = {
error:function(){},
warning:function(){}
}; //extend add a log element
jQuery.fn.debug = function(){
return this.each(function(){
alert(this);
});
};//add a jquery function
$("#id").debug(); // call the new added jquery function
>microsoft ajax library