设为首页 | 收藏本站
首页>IT学院>软件编程>正文
ASP.NET常用的三十三种实用代码
WWW.DBIT.CN 2007-12-3 8:27:19 热度:
 



  1.6 取当前分


int 分= DateTime.Now.Minute;


  1.7 取当前秒


int 秒= DateTime.Now.Second;


  1.8 取当前毫秒


int 毫秒= DateTime.Now.Millisecond;
28.自定义分页代码:

  先定义变量 :


public static int pageCount; //总页面数
public static int curPageIndex=1; //当前页面


  下一页:


if(DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
{
 DataGrid1.CurrentPageIndex += 1;
 curPageIndex+=1;
}

bind(); // DataGrid1数据绑定函数


  上一页:


if(DataGrid1.CurrentPageIndex >0)
{
 DataGrid1.CurrentPageIndex += 1;
 curPageIndex-=1;
}

bind(); // DataGrid1数据绑定函数


  直接页面跳转:


int a=int.Parse(JumpPage.Value.Trim());//JumpPage.Value.Trim()为跳转值

if(a<DataGrid1.PageCount)
{
 this.DataGrid1.CurrentPageIndex=a;
}

bind();

  29.DataGrid使用:

  添加删除确认:


private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 foreach(DataGridItem di in this.DataGrid1.Items)
 {
  if(di.ItemType==ListItemType.Item  di.ItemType==ListItemType.AlternatingItem)
  {
   ((LinkButton)di.Cells[8].Controls[0]).Attributes.Add("onclick","return confirm(’确认删除此项吗?’);");
  }
 }
}


  样式交替:


ListItemType itemType = e.Item.ItemType;

if (itemType == ListItemType.Item )
{
 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#FFFFFF’;";
 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ;
}
else if( itemType == ListItemType.AlternatingItem)
{
 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#a0d7c4’;";
 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ;
}


  添加一个编号列:


DataTable dt= c.ExecuteRtnTableForAccess(sqltxt); //执行sql返回的DataTable
DataColumn dc=dt.Columns.Add("number",System.Type.GetType("System.String"));

for(int i=0;i<dt.Rows.Count;i++)
{
 dt.Rows[i]["number"]=(i+1).ToString();
}

DataGrid1.DataSource=dt;
DataGrid1.DataBind();


  DataGrid1中添加一个CheckBox,页面中添加一个全选框


private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
{
 foreach(DataGridItem thisitem in DataGrid1.Items)
 {
  ((CheckBox)thisitem.Cells[0].Controls[1]).Checked=CheckBox2.Checked;
 }
}


  将当前页面中DataGrid1显示的数据全部删除


foreach(DataGridItem thisitem in DataGrid1.Items)
{
 if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked)
 {
  string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString();
  Del (strloginid); //删除函数
 }
}


  30.当文件在不同目录下,需要获取数据库连接字符串(如果连接字符串放在Web.config,然后在Global.asax中初始化)

  在Application_Start中添加以下代码:


Application["ConnStr"]=this.Context.Request.PhysicalApplicationPath+ConfigurationSettings.
   AppSettings["ConnStr"].ToString();


  31. 变量.ToString()

  字符型转换 转为字符串


12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000

本新闻共7页,当前在第6页  1  2  3  4  5  6  7  

 
上一篇:没有了
下一篇:编程中获取天气预报的源代码
打印】【关闭
  相关文章:
·Asp.Net输出数据到EXCEL表格中·CSS在ASP.NET中失效的原因及解决 
·ASP.NET中的命名空间·ASP.NET2.0数据库入门之SQL Serve
·安装ASP.NET运行环境·ASP.NET 2.0后台代码与内联代码的
☆联姻学院☆

Windows XP回收站无法清空解决
win xp下U盘不显示盘符的解决
网银盗号木马病毒的原理与防杀
关于木马病毒的六种启动方式
PowerPoint做交互课件
Excel中频繁提示“无法清空剪
在Windows系统中跟踪IP地址
Linux系统的服务器优化及安全
平面设计中的常用字库种类说明
Photoshop cs2在Vista下的尴尬
Dreamweaver架设网站高级教程
Dreamweaver制作网页的七大绝
ASP.NET常用的三十三种实用代
编程中获取天气预报的源代码
有线网卡转无线网卡的另类妙招
无线WiMAX和Wi-Fi的常见知识误

精品图秀
美女面相大盘点
清新孙俪纯情写真
刘羽琦最新写真
数码相机配件选购误区 爱国者MP5-E898入驻三 尼康广角相机上市 东软电脑城店庆日 惠士嘉快乐音符魅力重