常用的功能實現代碼

1.彈出對話框
  Response.Write("<script language='javascript'>alert('產品添加成功!')</script >");

2.彈出對話框.點擊轉向指定頁面
  Response.Write("<script>window.alert('該會員沒有提交申請,請重新提交!')</script>");
  Response.Write("<script>window.location ='http://www.msproject.cn/index.asp'</script>");

3.前臺驗證
 <script language="javascript" type="text/javascript">
 function check()
 {
   if(document.getElementById('<%=user_name.ClientID%>').value=="")
   {
     alert('回覆人不能爲空!');
     return false;
   }
   return true;
 }
</script>

4.//關閉按鈕,刷新一頁
 <a href="/admin/login.aspx" onclick="javascript:window.open('/admin/login.aspx','winopen','width=300,height=161');return false">
                                                                <img src="/img/common/admin.gif" width="34" height="18" border="0" /></a>
 Response.Write("<script>window.opener.location.replace(opener.location);window.close();</script>");

5.時間去秒顯示
 <%# System.DateTime.Parse(DataBinder.Eval(Container.DataItem,"begtime").ToString()).ToShortDateString()%>

6.刪除彈出確定按鈕
  <a id="btnDelete" onclick="return confirm('你是否確定刪除這條記錄嗎?');" href="#">刪除</a>

7.輸出數據格式化 "{0:F2}" 是格式 F2表示小數點後剩兩位
  <%# DataBinder.Eval(Container, "DataItem.PriceMoney","{0:F2}") %>

8.獲取" . "後面的字符
 i.ToString().Trim().Substring(i.ToString().Trim().LastIndexOf(".")+1).ToLower().Trim();

9.回車轉換成Tab
  <script language="javascript" for="document" event="onkeydown">
    if(event.keyCode==13 && event.srcElement.type!=’button’ && event.srcElement.type!=’submit’ && event.srcElement.type!=’reset’ && event.srcElement.type!=’’&& event.srcElement.type!=’textarea’)
      event.keyCode=9;
 </script>

 10.DataGrid行隨鼠標變色
  private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
    if (e.Item.ItemType!=ListItemType.Header)
    {
      e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=""+e.Item.Style["BACKGROUND-COLOR"]+""");
      e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=""+ "#EFF3F7"+""");
    }
  }

11.數字格式化
  【<%#Container.DataItem("price")%>的結果是500.0000,怎樣格式化爲500.00?】
   <%#Container.DataItem("price","{0:¥#,##0.00}")%>
   int i=123456;
   string s=i.ToString("###,###.00");  //生成 123,456.00
   12345.ToString("n"); //生成 12,345.00
   12345.ToString("C"); //生成 ¥12,345.00
     string.Format("{0:yyyy-MM-dd}",Convert.ToDateTime(dr["BOOKING_DATE"].ToString()));  //2009-09-26
Label10.Text = string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23?

Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);   //yyyymm等可以設置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);


12.大小寫轉換
  HttpUtility.HtmlEncode(string);
  HttpUtility.HtmlDecode(string);

13.GridView三種取值情況

   * <1>ReadOnly = true;  this.GridView1.Rows[e.RowIndex].Cells[0].Text
   * <2>TextBox           ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
   * <3>模板列            this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtTitle");

14.截取標題
  public string SubStringDescription(string description)
    {
        if (description.Length > 8)
            description = description.Substring(0, 8) + "....";
        return description;
    }
 
15.諮詢 參考網址:http://www.dcfly.cn/a.html 生成msn代碼http://settings.messenger.live.com/applications/CreateHtml.aspx
QQ:   
<a target="_blank" href="http://wpa.qq.com/msgrd?V=1&Uin=48279385&Site=chuanshenme.com&Menu=No">
       <table width="70px" cellpadding="0" cellspacing="0" border="0">
          <tr onclick="window.open('http://wpa.qq.com/msgrd?V=1&Uin=48279385&Site=chuanshenme.com&Menu=No');"
                style="cursor: hand;">
             <td width="23px" align="center" height="20px" valign="middle">
                <img border="0" src="http://wpa.qq.com/pa?p=1:48279385:4" height="16px" align="absmiddle" />
             </td>
             <td width="57px" align="left" height="20px" valign="middle">
               <img src="../img/zixun/zixun1_03.gif" alt="qq" />
             </td>
          </tr>
       </table>
   </a>
MSN:
 <a target="_blank" href="http://settings.messenger.live.com/Conversation/[email protected]&mkt=zh-cn">
   <table width="77px" cellpadding="0" cellspacing="0" border="0">
      <tr onclick="window.open('http://settings.messenger.live.com/Conversation/[email protected]&mkt=zh-cn','winopen','width=500,height=500');"
  style="cursor: hand;">
  <td width="30px" align="right" height="20px" valign="middle">
     <img border="0" src="http://messenger.services.live.com/users/[email protected]/presenceimage?mkt=zh-cn"
  height="16px" align="right" /></td>
     <td width="47px" align="left" height="20px" valign="middle">
     <img src="../img/quick/zixun3_03.gif" alt="qq" />
 </td>
   </tr>
</table>
</a>


//TransmitFile實現下載
    protected void Button1_Click(object sender, EventArgs e)
     {
      

         Response.ContentType = "application/x-zip-compressed";
         Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
        string filename = Server.MapPath("DownLoad/aaa.zip");
         Response.TransmitFile(filename);
     }

    //WriteFile實現下載
    protected void Button2_Click(object sender, EventArgs e)
     {
      

        string fileName ="aaa.zip";//客戶端保存的文件名
        string filePath=Server.MapPath("DownLoad/aaa.zip");//路徑

         FileInfo fileInfo = new FileInfo(filePath);
         Response.Clear();
         Response.ClearContent();
         Response.ClearHeaders();
         Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
         Response.AddHeader("Content-Length", fileInfo.Length.ToString());
         Response.AddHeader("Content-Transfer-Encoding", "binary");
         Response.ContentType = "application/octet-stream";
         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
         Response.WriteFile(fileInfo.FullName);
         Response.Flush();
         Response.End();
     }

    //WriteFile分塊下載
    protected void Button3_Click(object sender, EventArgs e)
     {

        string fileName = "aaa.zip";//客戶端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑

         System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

        if (fileInfo.Exists == true)
         {
            const long ChunkSize = 102400;//100K 每次讀取文件,只讀取100K,這樣可以緩解服務器的壓力
            byte[] buffer = new byte[ChunkSize];

             Response.Clear();
             System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
            long dataLengthToRead = iStream.Length;//獲取下載的文件總大小
             Response.ContentType = "application/octet-stream";
             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
            while (dataLengthToRead > 0 && Response.IsClientConnected)
             {
                int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小
                 Response.OutputStream.Write(buffer, 0, lengthRead);
                 Response.Flush();
                 dataLengthToRead = dataLengthToRead - lengthRead;
             }
             Response.Close();
         }
     }

    //流方式下載
    protected void Button4_Click(object sender, EventArgs e)
     {
        string fileName = "aaa.zip";//客戶端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑

        //以字符流的形式下載文件
         FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
         fs.Read(bytes, 0, bytes.Length);
         fs.Close();
         Response.ContentType = "application/octet-stream";
        //通知瀏覽器下載文件而不是打開
         Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
         Response.BinaryWrite(bytes);
         Response.Flush();
         Response.End();

     }

iframe滾動條顏色(
      
<style  type="text/css">
BODY

  ScrollBar-arrow-color:滾動條上下三角按鈕  
  ScrollBar-3d-light-color:立體滾動條亮邊的顏色  
  ScrollBar-base-color:基本顏色  
  ScrollBar-face-color:凸出部分顏色  
  ScrollBar-dark-shadow-color:強陰影的顏色  
  ScrollBar-light-color:空白部分的顏色  
  ScrollBar-shadow-color:陰影的顏色              
}
</style>
     但事實上,iframe滾動條顏色根本不會發生改變,爲什麼呢,是代碼最上面的<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">這句內嵌代碼在作怪,把這句代碼刪除,刷新頁面,iframe滾動條顏色發生變化,但是新的問題出現了,原來定義在body中的字體大小定義不了,怎麼辦呢。很簡單,把字體大小定義在table中就可以了。


   link:連接平常的狀態  
   active:連接被按下的時候  
   visited:連接被訪問過之後  
   hover:鼠標放到連接上的時候

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章