常用小知识总结

简介: 1、在数字统计中,经常会遇到类似67.666666%等的显示问题,其实我们想要得到的是67.

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、鼠标经过时弹出图片,移出后消失的简单效果代码:

html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title> <mce:script type="text/javascript"><!-- function showImage(obj){ var e = e||window.event; var div = document.getElementById("Image"); var path = obj.innerHTML; div.innerHTML="<img src=""+path+"" mce_src="&quot;+path+&quot;" width='100' height='100'/>"; div.style.display="block"; div.style.top = e.clientY; div.style.left=e.clientX; } function hideImage(){ document.getElementById("Image").style.display="none"; } // --></mce:script></head><body> <form id="form1" runat="server"> <div id="Image" style="position:absolute;width:100px;height:100px;border:1px solid red;display:none;"></div> <div> <asp:DataList ID="DataList1" runat="server"> <HeaderTemplate><table></HeaderTemplate> <ItemTemplate> <tr> <td><span onmouseover="showImage(this)" onmouseout="hideImage()"><%#Eval("Path")%></span></td> <td><img src='<%#Eval("Path") %>' /></td> </tr> </ItemTemplate> <HeaderTemplate></table></HeaderTemplate> </asp:DataList> </div> </form></body></html>后台cs代码protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataList1.DataSource = getDataTable(); DataList1.DataBind(); } } public DataTable getDataTable() { string[] name = { "images/p002.jpg", "images/p003.jpg", "images/p004.jpg" }; DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(System.Int32)); dt.Columns.Add("Path", typeof(System.String)); for (int i = 0; i < name.Length; i++) { DataRow row = dt.NewRow(); row[0] = i + 1; row[1] = name[i]; dt.Rows.Add(row); } return dt; }

 

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

int type = 0;if(!int.TryParse(Request.QueryString["type"], out type){ Response.Write("不合法"); return;}

 

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 m in 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;//路径  

相关文章
|
搜索推荐
暗网你了解吗?
暗网是什么?这些网站是什么时候开始发展的呢?网络有几层?
3361 0
|
运维 大数据 数据库管理
|
7天前
|
NoSQL Cloud Native Redis
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
阿里云瑶池数据库团队后续将持续参与Valkey社区,如过往在Redis社区一样耕耘,为开源社区作出持续贡献。
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
|
7天前
|
关系型数据库 分布式数据库 数据库
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
PolarDB分布式版助力《香肠派对》实现百亿好友关系20万QPS的毫秒级查询。
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
|
8天前
|
消息中间件 Cloud Native Serverless
RocketMQ 事件驱动:云时代的事件驱动有啥不同?
本文深入探讨了云时代 EDA 的新内涵及它在云时代再次流行的主要驱动力,包括技术驱动力和商业驱动力,随后重点介绍了 RocketMQ 5.0 推出的子产品 EventBridge,并通过几个云时代事件驱动的典型案例,进一步叙述了云时代事件驱动的常见场景和最佳实践。
115073 1
|
9天前
|
弹性计算 安全 API
访问控制(RAM)|云上安全使用AccessKey的最佳实践
集中管控AK/SK的生命周期,可以极大降低AK/SK管理和使用成本,同时通过加密和轮转的方式,保证AK/SK的安全使用,本次分享为您介绍产品原理,以及具体的使用步骤。
101841 3
|
8天前
|
自然语言处理 Cloud Native Serverless
通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
近日,通义灵码正式进驻函数计算 FC WebIDE,让使用函数计算产品的开发者在其熟悉的云端集成开发环境中,无需再次登录即可使用通义灵码的智能编程能力,实现开发效率与代码质量的双重提升。
95414 2
|
5天前
|
物联网 PyTorch 测试技术
手把手教你捏一个自己的Agent
Modelscope AgentFabric是一个基于ModelScope-Agent的交互式智能体应用,用于方便地创建针对各种现实应用量身定制智能体,目前已经在生产级别落地。
|
1天前
|
NoSQL Java Redis
使用Redis实例搭建网上商城的商品相关性分析程序
本教程将指导您如何快速创建实例并搭建网上商城的商品相关性分析程序。(ApsaraDB for Redis)是兼容开源Redis协议标准的数据库服务,基于双机热备架构及集群架构,可满足高吞吐、低延迟及弹性变配等业务需求。
17087 0
Doodle Jump — 使用Flutter&Flame开发游戏真不错!
用Flutter&Flame开发游戏是一种什么体验?最近网上冲浪的时候,我偶然发现了一个国外的游戏网站,类似于国内的4399。在浏览时,我遇到了一款经典的小游戏:Doodle Jump...
112751 12

热门文章

最新文章