aspx
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
Text
=
"
Button
"
OnClick
=
"
Button1_Click
"
/>
</
form
>
cs
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
ClientScriptManager cs
=
this
.ClientScript;
cs.RegisterArrayDeclaration(
"
Hello
"
,
"
1, 2, 3
"
);
cs.RegisterClientScriptBlock(
this
.GetType(),
"
HelloWorld
"
,
"
function helloWorld(){alert(1);}
"
,
true
);
cs.RegisterClientScriptInclude(
"
HelloWorld
"
,
"
HelloWorld.js
"
);
//
cs.RegisterClientScriptResource(
cs.RegisterExpandoAttribute(
this
.Button1.ClientID,
"
Hello
"
,
"
World
"
);
cs.RegisterHiddenField(
"
hello
"
,
"
world
"
);
cs.RegisterOnSubmitStatement(
this
.GetType(),
"
HelloWorld
"
,
"
return window.confirm('Do you really want to submit the form?')
"
);
cs.RegisterStartupScript(
this
.GetType(),
"
HelloWorld
"
,
"
<script>alert('The page has loaded!')</script>
"
);
}
点击Button1后生成的html
<
form name
=
"
form1
"
method
=
"
post
"
action
=
"
ClientScriptManager.aspx
"
onsubmit
=
"
javascript:return WebForm_OnSubmit();
"
id
=
"
form1
"
>
<
div
>
<
input type
=
"
hidden
"
name
=
"
hello
"
id
=
"
hello
"
value
=
"
world
"
/>
<
input type
=
"
hidden
"
name
=
"
__VIEWSTATE
"
id
=
"
__VIEWSTATE
"
value
=
"
/wEPDwUKMTQ2OTkzNDMyMWRkXQusG/GEJI5thnlOYBE9TxuzDXA=
"
/>
</
div
>
<
script type
=
"
text/javascript
"
>
//
<![CDATA[
function helloWorld(){alert(
1
);}
//
]]>
</
script
>
<
script src
=
"
HelloWorld.js
"
type
=
"
text/javascript
"
></
script
>
<
script type
=
"
text/javascript
"
>
//
<![CDATA[
function WebForm_OnSubmit() {
return
window.confirm(
'
Do you really want to submit the form?
'
);
return
true
;
}
//
]]>
</
script
>
<
input type
=
"
submit
"
name
=
"
Button1
"
value
=
"
Button
"
id
=
"
Button1
"
/>
<
script type
=
"
text/javascript
"
>
//
<![CDATA[
var Hello
=
new
Array(
1
,
2
,
3
);
//
]]>
</
script
>
<
script type
=
"
text/javascript
"
>
//
<![CDATA[
var Button1
=
document.all
?
document.all[
"
Button1
"
] : document.getElementById(
"
Button1
"
);
Button1.Hello
=
"
World
"
;
//
]]>
</
script
>
<
div
>
<
input type
=
"
hidden
"
name
=
"
__EVENTVALIDATION
"
id
=
"
__EVENTVALIDATION
"
value
=
"
/wEWAgLg9frACwKM54rGBnQtBc2fVh5CkTBe8MjX+hmuyng2
"
/>
</
div
>
<
script
>
alert(
'
The page has loaded!
'
)
</
script
></
form
>
注意使用
RegisterStartupScript方法生成的javascript是添加到</form>标签前面的
而是用
RegisterClientScriptBlock方法成的javascript是添加到<form>后面的,生成的位置不一样