常用小知识总结 .

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

  1. 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类型参数,为防止用户非法修改参数的一个解决办法:

  1. 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();

posted @ 2011-11-02 16:36  你妹的sb  阅读(266)  评论(0编辑  收藏  举报
百度一下