JavaScript with ASP.NET 2.0 Pages - Part 1
2007-01-15 11:03
267 查看
原址: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());
}
第2 种是直接调用服务器方法:
TextBox1.Focus()
第3种是在form里设置(这个我还是第一次见)
<form defaultfocus="textbox2" runat="server">
<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>
引入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"));
}
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());
}
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>
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>
引入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"));
}
相关文章推荐
- ASP.NET MVC- VIEW Creating Page Layouts with View Master Pages Part 4
- Working with Data in ASP.NET 2.0 :: Master Pages and Site Navigation
- [翻译]在 ASP.NET 2.0 中高效率地使用 JavaScript - Part 1
- Client Side Validation with JavaScript in ASP.NET
- How to take screenshot (thumbnail) of a web site with ASP.NET 2.0?
- ASP.NET 2.0的Web Part Framework(新书连载)
- Ajax with ASP.NET--含常用javascript(转)
- 解决错误:sql_variant is incompatible with xml (ASP.NET 2.0 / XML数据类型 )
- How To: Use Forms Authentication with SQL Server in ASP.NET 2.0
- Asp.net 2.0控件开发相关调试(JavaScript调试和自定义控件设计时调试)
- Building a Web 2.0 Portal with asp.net ajax 第一章
- ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换
- Asp.net 2.0控件开发相关调试(JavaScript调试和自定义控件设计时调试)
- ASP.NET 2.0: Implementing Single Sign On (SSO) with Membership API
- FLV Flash video streaming with ASP.NET 2.0, IIS and HTTP handler [转载]
- Asynchronous Pages in ASP.NET 2.0
- 2006年11月9日 Working with Data and ASP.NET 2.0
- ASP.NET 2.0 Language Swithcer and Theme Swicher
- 【转贴】考察ASP.NET 2.0的Membership, Roles, and Profile - Part 7
- htc 的应用--htc解释:运用css+javascript方便的实现一些控件的特效+结合asp.net 2.0的应用