My ASP.NET 2.0 code snippets

1. 常用的货币格式化字符串

string  s  =  salary.ToString( " ##,##0.00 " );

2. GridView 中日期、数字的格式化,需要加一个 HtmlEncode="false" 才能奏效。

日期:
< asp:BoundField  DataField ="FromDate"  HeaderText ="开始时间"  DataFormatString ="{0:yyyy-MM-dd}"  HtmlEncode ="false"   />

数字:
< asp:BoundField  DataField ="平均分"  HeaderText ="平均分"  DataFormatString ="{0:##,##0.00}"  HtmlEncode ="false"   />

3. GridView 中带确认信息的 DeleteButton

< asp:TemplateField >
    
< ItemTemplate >
        
< asp:LinkButton  ID ="LinkButton1"  runat ="server"  CommandName ="Delete"  OnClientClick ="return confirm('您确认要删除吗?')"   CausesValidation ="false" > 删除 </ asp:LinkButton >
    
</ ItemTemplate >
</ asp:TemplateField >

4. 典型的时间输入的校验

< asp:TextBox  ID ="txtFromDate"  runat ="server"  ValidationGroup ="vgp1"   /> (格式:1978-10-11)
< asp:RequiredFieldValidator  ID ="reqFromDate"  runat ="server"
     ControlToValidate
="txtFromDate"
     ErrorMessage
="请输入开始时间"
     ValidationGroup
="vgp1"    />
< asp:CustomValidator  ID ="cvFromDate"  runat ="server"
    ControlToValidate
="txtFromDate"
     OnServerValidate
="Validate_Date"
     ErrorMessage
="开始时间的格式不正确。(例:1978-01-02)"
     ValidationGroup
="vgp1"  
/>

< script language = " javascript "  type = " text/javascript " >
//  客户端日期验证函数
function  Validate_Date_Client(src, e)
{
    e.IsValid 
=   /^ \d{ 4 } - \d{ 2 } - \d{ 2 }$ / .test(e.Value);
}
</ script >  

//  服务器端日期验证
protected   void  Validate_Date( object  source, ServerValidateEventArgs e)
{        
    e.IsValid 
=   true ;
    
try
    {
        DateTime dt 
=  DateTime.Parse(e.Value);            
    }
    
catch
    {
        e.IsValid 
=   false ;
    }
}

你可能感兴趣的:(asp.net)