ASP.NET程序中常用代码汇总(五)

41.判断是否为数字

/**/ ///

1<summary>   
2![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ///  名称:IsNumberic   
3![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ///  功能:判断输入的是否是数字   
4![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ///  参数:string oText:源文本   
5![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ///  返回值: bool true:是 false:否   
6![](http://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif) ///  </summary>


public bool IsNumberic( string oText)
{
try
{
int var1 = Convert.ToInt32 (oText);
return true ;
}
catch
{
return false ;
}
}

获得字符串实际长度(包括中文字符)

// 获得字符串oString的实际长度
public int StringLength( string oString)
{
byte [] strArray = System.Text .Encoding.Default .GetBytes (oString);
int res = strArray.Length ;
return res;
}

42.将回车转换为TAB

// 当在有keydown事件的控件上敲回车时,变为tab
public void Tab(System.Web .UI.WebControls .WebControl webcontrol)
{
webcontrol.Attributes .Add ( " onkeydown " , " if(event.keyCode==13) event.keyCode=9 " );
}

43.datagrid分页中如果删除时出现超出索引

public void jumppage(System.Web.UI.WebControls.DataGrid dg)
{
int int_PageLess; // 定义页面跳转的页数
// 如果当前页是最后一页
if (dg.CurrentPageIndex == dg.PageCount - 1 )
{
// 如果就只有一页
if (dg.CurrentPageIndex == 0 )
{
// 删除后页面停在当前页
dg.CurrentPageIndex = dg.PageCount - 1 ;
}
else
{
// 如果最后一页只有一条记录
if ((dg.Items.Count % dg.PageSize == 1 ) || dg.PageSize == 1 )
{
// 把最后一页最后一条记录删除后,页面应跳转到前一页
int_PageLess = 2 ;
}
else // 如果最后一页的记录数大于1,那么在最后一页删除记录后仍然停在当前页
{
int_PageLess = 1 ;
}
dg.CurrentPageIndex = dg.PageCount - int_PageLess;
}
}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus