asp.net学习笔记(转)

1. %=和%#的区别
%=表示从后台获取数据,而%#也是从后台获取数据,不过它的数据必须DataBind后才生效

2. 设置焦点
usernameTextBox.Focus();3. 服务器端构造JS代码的一种方法
RaiseCallbackEvent,处理从页面上的JS脚本传递过来的请求和数据,处理后,再将结果传回给页面。GetPostBackEventReference,获取对客户端脚本函数的引用,调用该函数将使服务器发送回该页。该方法还将一个参数传递到在服务器上执行回发处理的服务器控件。
(Page.GetPostBackEventReference(Button1,"inc") == "__doPostBack('Button1','inc')", Button1是Button1的ID)
范例1:
public class MyControl : Control, IPostBackEventHandler...{   public void RaisePostBackEvent(string eventArgument)   ...{     if ( eventArgument == "inc" )     Number = Number + 1;     if ( eventArgument == "dec" )     Number = Number - 1;   }   [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]   protected override void Render(HtmlTextWriter writer)   ...{     writer.Write("The Number is " + Number.ToString() + " (" );     writer.Write("<a href="javascript:" + Page.GetPostBackEventReference(this,"inc") + "">Increase Number</a>");      writer.Write(" or ");     writer.Write("<a href="javascript:" + Page.GetPostBackEventReference(this,"dec") + "">Decrease Number</a>");   }}

范例2:绑定控件的ENTER事件到特定控件上public static void TieButton(Page page, Control TextBoxToTie, Control ButtonToTie)                                                                                                                     ...{  string jsString = "";  if ((ButtonToTie is LinkButton) || (ButtonToTie is ImageButton))  ...{      jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"               + page.ClientScript.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$")               + ";return false;} else return true;";  }  else  ...{      jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."             + "forms[0].elements['" + ButtonToTie.UniqueID.Replace(":", "_")             + "'].click();return false;} else return true; ";  }  // Attach jscript to the onkeydown attribute—we have to cater for HtmlControl or WebControl  if (TextBoxToTie is HtmlControl)  ...{  ((HtmlControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);  }  else if (TextBoxToTie is WebControl)  ...{  ((WebControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);  }}
4. 页面构造时控件初始化顺序
Master Page child controls initialization.Content Page child controls initialization.Master page initialization.Content Page initialization.Content Page load.Master Page load.Master Page child controls load.Content Page child controls load.5. HttpApplication常用事件顺序
BeginRequest 在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生 AuthenticateRequest 当安全模块已建立用户标识时发生。 AuthorizeRequest 当安全模块已验证用户授权时发生。IHttpModule进行URL重写请在你关注的阶段注册相应事件,如public void Init(HttpApplication application) {  application.AuthorizeRequest += new EventHandler(this.Application_AuthorizeRequest);}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/anghlq/archive/2006/09/18/1238588.aspx

posted @ 2010-08-17 17:33  zhdonghu  阅读(169)  评论(0编辑  收藏  举报