原址:JavaScript with ASP.NET 2.0 Pages - Part 1
P:觉得这篇文章写的很不错,所以在这里汇总一下,英文好的最好直接去上面的网址看
1、聚焦到某个服务器控件(3 种方法)
//
ASP 1.x method of setting focus to a specific control. You need
// to first pass the control's id as the parameter, then define the
// JavaScript function in a string variable then call the Page class
// to register the script. You may use either RegisterStartupScript()
// or RegisterClientScriptBlock() method.
private void SetFocus(String ctrlID)
{
// Build the JavaScript String
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append( " <script language='javascript'> " );
sb.Append( " document.getElementById(' " );
sb.Append(ctrlID);
sb.Append( " ').focus() " );
sb.Append( " </script> " )
// Register the script code with the page.
Page.RegisterStartupScript( " FocusScript " , sb.ToString());
}
// to first pass the control's id as the parameter, then define the
// JavaScript function in a string variable then call the Page class
// to register the script. You may use either RegisterStartupScript()
// or RegisterClientScriptBlock() method.
private void SetFocus(String ctrlID)
{
// Build the JavaScript String
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append( " <script language='javascript'> " );
sb.Append( " document.getElementById(' " );
sb.Append(ctrlID);
sb.Append( " ').focus() " );
sb.Append( " </script> " )
// Register the script code with the page.
Page.RegisterStartupScript( " FocusScript " , sb.ToString());
}
void Page_Init(object sender, EventArgs e)
{
SetFocus(ControlToSetFocus);
}
第2 种是直接调用服务器方法:
TextBox1.Focus()
第3种是在form里设置(这个我还是第一次见)
<form defaultfocus="textbox2" runat="server">
<asp:textbox id="textbox1" runat="server"/>
<asp:textbox id="textbox2" runat="server"/>
</form>
<asp:textbox id="textbox1" runat="server"/>
<asp:textbox id="textbox2" runat="server"/>
</form>
2、加载客户端代码 (可能会经常用到)
//
Set the button's client-side onmouseover event
btnClick.Attributes.Add(
"
onClick
"
,
"
alert('Ouch, you clicked me!');
"
);
我想应该相当于ClientClick的属性吧,不过我想说的是,既然可以加入onclick,那么应该也能加入onfocus之类的client-script 吧
3、Maintaining Page Scrolling Position
<%@ Page ... MaintainScrollPositionOnPostback="true" %>
只要设置这个属性,postback之后就会回到原来的位置(假如有滚动条的话),而不是最顶的地方
随时注入脚本
<
script runat
=
"
server
"
>
protected void btnPopUp_Click( object sender, EventArgs e)
{
// Build a Pop Up JavaScript
// please note the peculiar '/script' in the last line of the script string
// This is to work around the problem that compiler would mistake the
// closing script tag as the outer script closing tag.
// For details, please see
// http://support.microsoft.com/default.aspx?scid=kb ;EN-US;827420
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append( " <script language='javascript'> " );
sb.Append( " window.open('javascript.htm', 'CustomPopUp', " );
sb.Append( " 'width=200, height=200, menubar=yes, resizable=no');< " );
sb.Append( " /script> " );
// register with ClientScript
// The RegisterStartupScript method is also slightly different
// from ASP.NET 1.x
Type t = this .GetType();
if ( ! ClientScript.IsClientScriptBlockRegistered(t, " PopupScript " ))
ClientScript.RegisterClientScriptBlock(t, " PopupScript " , sb.ToString());
}
</ script >
< form id = " form2 " runat = " server " >
< asp:Button ID = " btnPopUp " runat = " server "
Text = " PopUp " OnClick = " btnPopUp_Click " />
</ form >
protected void btnPopUp_Click( object sender, EventArgs e)
{
// Build a Pop Up JavaScript
// please note the peculiar '/script' in the last line of the script string
// This is to work around the problem that compiler would mistake the
// closing script tag as the outer script closing tag.
// For details, please see
// http://support.microsoft.com/default.aspx?scid=kb ;EN-US;827420
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append( " <script language='javascript'> " );
sb.Append( " window.open('javascript.htm', 'CustomPopUp', " );
sb.Append( " 'width=200, height=200, menubar=yes, resizable=no');< " );
sb.Append( " /script> " );
// register with ClientScript
// The RegisterStartupScript method is also slightly different
// from ASP.NET 1.x
Type t = this .GetType();
if ( ! ClientScript.IsClientScriptBlockRegistered(t, " PopupScript " ))
ClientScript.RegisterClientScriptBlock(t, " PopupScript " , sb.ToString());
}
</ script >
< form id = " form2 " runat = " server " >
< asp:Button ID = " btnPopUp " runat = " server "
Text = " PopUp " OnClick = " btnPopUp_Click " />
</ form >
引入javascript (应该可以支持“~/MyJavaScript.js”这种形式的)
ClientScript.
RegisterClientScriptInclude("MyScript","MyJavaScript.js")
相当于html code的:
<script src="MyJavaScript.js" type="text/javascript"></script>
绑定js文件到资源文件中
[assembly: WebResource("{namespace}.{filename}", "{content-type}")]
在这个例子使用:
[assembly: WebResource("WebControlLibrary1.AutoComplete.js", "text/javascript")]
使用资源文件的:
protected
override
void
OnPreRender(EventArgs e)
{
base .OnPreRender(e);
this .Page.ClientScript.RegisterClientScriptInclude( " AutoComplete " ,
this .Page.ClientScript.GetWebResourceUrl( typeof (AutoCompleteDropDownList),
" WebControlLibrary1.AutoComplete.js " ));
}
{
base .OnPreRender(e);
this .Page.ClientScript.RegisterClientScriptInclude( " AutoComplete " ,
this .Page.ClientScript.GetWebResourceUrl( typeof (AutoCompleteDropDownList),
" WebControlLibrary1.AutoComplete.js " ));
}
1173

被折叠的 条评论
为什么被折叠?



