现在的位置: 首页 > 综合 > 正文

常用小知识总结

2012年06月21日 ⁄ 综合 ⁄ 共 6353字 ⁄ 字号 评论关闭

1、在数字统计中,经常会遇到类似67.666666%等的显示问题,其实我们想要得到的是67.67%就可以了,但是我们用

val.ToString("F2")的话,那么100%就会显示成100.00%,不是很好看,怎么办呢?

以下介绍一种方法:

Math.Round(参数列表)方法

//实际得分占标准分比例
    public string getPercent(string text1, string text2)
    {

         double t1,t2,result;

         try

        {
                t1 = double.Parse(text1.Trim().ToString());
                t2 = double.Parse(text2.Trim().ToString());
                result = double.Parse(((t1 / t2) * 100).ToString());
        }

         catch(Exception)

        {

             ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('字符格式不正确');</script>");

              return;

         }

         return Math.Round(result, 2, MidpointRounding.AwayFromZero).ToString()+"%";
    }

 

2、很简单的一个倒计时,进入某个页面的方法

其实重点的就是利用JavaScript里的一个setInterval函数,下面写下该简单的额js脚本

 

<script type="text/javascript">
        var time = 10;
        function count() {
            if (time > 0) {
                document.getElementById("dzsj").innerHTML = time;
                time--;
            }
            else if (time == 0) {
                window.location.href = "CompanyNews/gsxw.aspx";
                clearInterval(timer);
            }
        }
        timer = setInterval('count()', 1000);  //第一个是函数,需用' '引起来,第二个参数是毫秒值(11000
    </script>

 

<div>

        系统将在<span id="dzsj" style="font-weight: bold; color: Red;"></span>秒内跳转到公司新闻页面!

</div>

3、定时页面跳转

ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert(/'响应启动成功!/');setTimeout(function(){location.href='URFeedBack.aspx'},1000);  </script>");

 

4、鼠标经过时弹出图片,移出后消失的简单效果代码:

 

5、地址栏中传递int类型参数,为防止用户非法修改参数的一个解决办法:

 

6、限时抢购代码

团购倒计时抢购功能(JS代码) JS代码如下:  
<SCRIPT LANGUAGE="JavaScript">  
function _fresh()  
{  
 var endtime=new Date("2010/11/05,12:20:12");  
 var nowtime = new Date();  
 var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000);  
 __d=parseInt(leftsecond/3600/24);  
 __h=parseInt((leftsecond/3600)%24);  
 __m=parseInt((leftsecond/60)%60);  
 __s=parseInt(leftsecond%60);  
 document.getElementById("times").innerHTML=__d+"天 "+__h+"小时"+__m+"分"+__s+"秒";  
 if(leftsecond<=0){  
 document.getElementById("times").innerHTML="抢购已结束";  
 clearInterval(sh);  
 }  
}  
_fresh()  
var sh;  
sh=setInterval(_fresh,1000);  
</SCRIPT>  
Html代码如下:  
<div id="content">  
<h1>限时抢购啦!</h1>  
<p>还剩<span id="times"></span></p>  
  
</div> 

 

7、字符串中中英文字符区分方法:

方法一:

string
 str 
=
 
"
放不开放不下taken in and fetch off
"
;

string
 str1 
=
 
new
 
string
(str.ToList().Where(x 
=>
 ((x 
>=
 
'
A
'
 
&&
 x 
<=
 
'
Z
'
) 
||
 (x 
>=
 
'
a
'
 
&&
 x 
<=
 
'
z
'
))).ToArray());

string
 str2 
=
 str.Replace(str1, 
""
);



方法二:


string rule
= "[/u4e00-/u9fa5]+[a-zA-Z//s]+";
System.Text.RegularExpressions.Regex reg
= new System.Text.RegularExpressions.Regex(rule );
string str="放不开放不下taken in and fetch off";
foreach (Match min reg.Matches(str))
{
    Console.WriteLine(m.Value);
}
8、WPF中定义TabControl的样式:

<
Window x:Class
=
"
CaseAnalysis_WPF_.Window1
"

        xmlns
=
"
http://schemas.microsoft.com/winfx/2006/xaml/presentation
"

        xmlns:x
=
"
http://schemas.microsoft.com/winfx/2006/xaml
"

        Title
=
"
Window1
"
 Height
=
"
300
"
 Width
=
"
300
"
>


<
Window.Resources
>

     
<
Style TargetType
=
"
{x:Type TabItem}
"
>

            
<
Setter Property
=
"
Template
"
>

                
<
Setter.Value
>

                    
<
ControlTemplate TargetType
=
"
{x:Type TabItem}
"
>

                        
<
Grid
>

                            
<
Border Name
=
"
Border
"
 Background
=
"
LightBlue
"
 BorderBrush
=
"
Black
"
 BorderThickness
=
"
1,1,1,1
"
 CornerRadius
=
"
6,6,0,0
"
>

                                
<
ContentPresenter x:Name
=
"
ContentSite
"
 VerticalAlignment
=
"
Center
"
 HorizontalAlignment
=
"
Center
"
 ContentSource
=
"
Header
"
 Margin
=
"
12,2,12,2
"
/>

                            
</
Border
>

                        
</
Grid
>

                        
<
ControlTemplate.Triggers
>

                            
<
Trigger Property
=
"
IsSelected
"
 Value
=
"
True
"
>

                                
<
Setter TargetName
=
"
Border
"
 Property
=
"
Background
"
 Value
=
"
Red
"
 
/>

                            
</
Trigger
>

                            
<
Trigger Property
=
"
IsSelected
"
 Value
=
"
False
"
>

                                
<
Setter TargetName
=
"
Border
"
 Property
=
"
Background
"
 Value
=
"
LightGray
"
 
/>

                            
</
Trigger
>

                        
</
ControlTemplate.Triggers
>

                    
</
ControlTemplate
>

                
</
Setter.Value
>

            
</
Setter
>

        
</
Style
>

    
</
Window.Resources
>

 
<
Grid
>

  
<
TabControl HorizontalAlignment
=
"
Left
"
 Name
=
"
tabControl1
"
 VerticalAlignment
=
"
Top
"
 Height
=
"
311
"
 Width
=
"
491
"
 TabStripPlacement
=
"
Left
"
 IsTextSearchEnabled
=
"
False
"
 IsManipulationEnabled
=
"
False
"
 Margin
=
"
-2,0,0,0
"
>

            
<
TabItem Name
=
"
tab1
"
 UseLayoutRounding
=
"
False
"
 Background
=
"
#FF41DBF7
"
 Width
=
"
25
"
 Height
=
"
60
"
 BorderBrush
=
"
Black
"
 Margin
=
"
0
"
>

    
</
TabItem
>

        
</
TabControl
>

  
</
Grid
>


</
Window
>

9、后台找前台html控件:
System.Web.UI.HtmlControls.HtmlInputCheckBox cb = row.FindControl("checkboxItem") as System.Web.UI.HtmlControls.HtmlInputCheckBox; 

10、数据绑定类型

(1)将HashTable数据源输出成一个数组,将该数组绑定;

(2)直接绑定,但是要指定DropDownListTextValue分别为HashTableKeyValue。

   例如:

  Hashtable    ht    =    new    Hashtable();       for(int    i=0;    i<10;    i++)       {       ht.Add(i+1,"Item"+1);       }       this.DropDownList1.DataSource    =    ht;       this.DropDownList1.DataTextField    =    "key";       this.DropDownList1.DataValueField    =    "value";            this.DropDownList1.DataBind();

关于DataList与HashTable数据绑定其实也挺简单,只需将HashTable的Key或者Value与控件绑定即可,例如:

  Hashtable ht = new Hashtable();          //对ht赋值    this.DataList1.DataSource = ht.Values;    this.DataList1.DataBind();

获取文件相关属性:

System.IO.FileInfo file = new System.IO.FileInfo(FileName); file.Name;//文件名 file.Length.ToString();//大小", file.LastAccessTime.ToString();//最后访问时间 file.LastWriteTime.ToString();//最后修改时间 file.DirectoryName;//路径  

抱歉!评论已关闭.