后台代码
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/11/05/1596601.html如需转载请自行联系原作者
1
2
3
4
5
6
7
8
9
|
private
string
getjQueryCode(
string
jsCodetoRun)
{
StringBuilder sb =
new
StringBuilder();
sb.AppendLine(
"$(document).ready(function() {"
);
sb.AppendLine(jsCodetoRun);
sb.AppendLine(
" });"
);
return
sb.ToString();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private
void
runjQueryCode(
string
jsCodetoRun)
{
ScriptManager requestSM = ScriptManager.GetCurrent(
this
);
if
(requestSM !=
null
&& requestSM.IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(
this
,
typeof
(Page),
Guid.NewGuid().ToString(),
getjQueryCode(jsCodetoRun),
true
);
}
else
{
ClientScript.RegisterClientScriptBlock(
typeof
(Page),
Guid.NewGuid().ToString(),
getjQueryCode(jsCodetoRun),
true
);
}
}
|
前台代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<asp:ScriptManager ID=
"sm"
runat=
"server"
>
</asp:ScriptManager>
<asp:UpdatePanel ID=
"upPnl"
runat=
"server"
>
<ContentTemplate>
<asp:Button ID=
"btnPostback"
runat=
"server"
Text=
"Standard Postback"
OnClick=
"btnPostback_Click"
/>
<asp:Button ID=
"btnAsynchPostback"
runat=
"server"
Text=
"Asynchronous Postback"
OnClick=
"btnAsynchPostback_Click"
/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID=
"btnPostback"
/>
</Triggers>
</asp:UpdatePanel>
|
调用示例
同步
1
2
3
4
5
6
7
8
9
|
protected
void
btnPostback_Click(
object
sender, EventArgs e)
{
runjQueryCode(
"alert('After a standard postback.')"
);
}
异步
protected
void
btnAsynchPostback_Click(
object
sender, EventArgs e)
{
runjQueryCode(
"alert('After an asynchronous postback.')"
);
}
|
王德水