代码改变世界

asp.net 2.0常用代码集合

2007-03-24 23:30  ruinet  阅读(497)  评论(0编辑  收藏  举报
获取客户端IP
System.Web.HttpContext.Current.Request.UserHostAddress;
动态创建GridView的列
GridView1.DataSourceID = "SqlDataSource1";
        GridView1.AutoGenerateColumns 
= false;
        GridView1.DataKeyNames 
= new string[] "EmployeeID" };
        GridView1.AllowPaging 
= true;
        GridView1.AllowSorting 
= true;
        GridView1.PageSize 
= 5;

        BoundField bf1 
= new BoundField();
        BoundField bf2 
= new BoundField();
        BoundField bf3 
= new BoundField();

        bf1.HeaderText 
= "Employee ID";
        bf1.DataField 
= "EmployeeID";
        bf1.ReadOnly 
= true;
        bf1.SortExpression 
= "EmployeeID";

        bf2.HeaderText 
= "First Name";
        bf2.DataField 
= "FirstName";
        bf2.SortExpression 
= "FirstName";

        bf3.HeaderText 
= "Last Name";
        bf3.DataField 
= "LastName";
        bf3.SortExpression 
= "LastName";

        CommandField cf 
= new CommandField();
        cf.ButtonType 
= ButtonType.Button;
        cf.ShowCancelButton 
= true;
        cf.ShowEditButton 
= true;

        GridView1.Columns.Add(bf1);
        GridView1.Columns.Add(bf2);
        GridView1.Columns.Add(bf3);
        GridView1.Columns.Add(cf);
打开新的窗口并传送参数:
response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="++"’)</script>")

1.父窗口中打开子窗口
 如:
 window.open ('children.aspx','newwindow','height=100,width=400,top=200,left=200,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')

 基本语法:
window.open(pageURL,name,parameters)
其中:
pageURL 为子窗口路径
name 为子窗口句柄
parameters 为窗口参数(各参数用逗号分隔)

参数 | 取值范围 | 说明

alwaysLowered | yes/no | 指定窗口隐藏在所有窗口之后
alwaysRaised | yes/no | 指定窗口悬浮在所有窗口之上
depended | yes/no | 是否和父窗口同时关闭
directories | yes/no | Nav2和3的目录栏是否可见
height | pixel value | 窗口高度
hotkeys | yes/no | 在没菜单栏的窗口中设安全退出热键
innerHeight | pixel value | 窗口中文档的像素高度
innerWidth | pixel value | 窗口中文档的像素宽度
location | yes/no | 位置栏是否可见
menubar | yes/no | 菜单栏是否可见
outerHeight | pixel value | 设定窗口(包括装饰边框)的像素高度
outerWidth | pixel value | 设定窗口(包括装饰边框)的像素宽度
resizable | yes/no | 窗口大小是否可调整
screenX | pixel value | 窗口距屏幕左边界的像素长度
screenY | pixel value | 窗口距屏幕上边界的像素长度
scrollbars | yes/no | 窗口是否可有滚动栏
titlebar | yes/no | 窗口题目栏是否可见
toolbar | yes/no | 窗口工具栏是否可见
Width | pixel value | 窗口的像素宽度
z-look | yes/no | 窗口被激活后是否浮在其它窗口之上

2.子窗体中获取对像并传递值:
  如:
   window.opener.document.getElementById('text1').value='子窗口传值';
3.在子窗体中关闭子窗体:
  self.close();
完毕。

为按钮添加确认提示对话框

Button1.Attributes.Add("onclick","return confirm(’确认?’)");
button.attributes.add(
"onclick","if(confirm(’are you sure?’)){return true;}else{return false;}")
获取错误信息并到指定页面
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer(
"MyErrorPage.aspx");

//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
}

清空Cookie
Cookie.Expires=[DateTime];
Response.Cookies(
"UserName").Expires = 0
将回车转换为TAB
//当在有keydown事件的控件上敲回车时,变为tab 
 public void Tab(System.Web .UI.WebControls .WebControl webcontrol) 
 

  webcontrol.Attributes .Add (
"onkeydown""if(event.keyCode==13) event.keyCode=9"); 
 }
 
设定全局变量
Global.asax中
  Application_Start()事件中
  添加Application[属性名] = xxx;
  就是你的全局变量
加密
在.net中提供两种加密方式MD5和SHA1
TextBox2.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text, "MD5");
Panel 横向滚动,纵向自动扩展
<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>


 


free web counter