2008年10月15日 星期三

時間格式轉換

0 意見

包仔最近在實作FTP存取時,擷取檔案被修改時間的時候發現在每台機器所架設的FTP
檔案時間擷取的時間都不一樣,且看包仔說明如下:

包仔最近在實作FTP存取時,擷取檔案被修改時間的時候發現在每台機器所架設的FTP
檔案時間擷取的時間都不一樣,且看包仔說明如下:

包仔目前為止在擷取時間格式時已出現了下列幾種組合:
1. Oct 1 08:00
2. Oct 01 08:00
3. Oct 01 2008
4. Oct 1 2008
5. Oct 01 08
6. Oct 1 08
7. 10-01-08 08:00AM

格式應該還有比較多種,目前只有測出以上七種

包仔解決方式如下



string TimeStamp = "抓取的時間戳記";
string[] ParseFormat = {"MMM d HH:mm",
"MMM dd HH:mm",
"MMM dd yyyy",
"MMM d yyyy",
"MMM dd yy",
"MMM d yy",
"MM-dd-yy HH:mmtt"};

string TimeStampTransfer = DateTime.ParseExact(TimeStamp,
ParseFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);


如此即可將時間格式轉換成 2008/10/01 上午 08:00:00

大家可以試看看喔!!...或許還有別種轉換方式哩!!
... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年5月22日 星期四

正規式擷取特定格式資料

0 意見

包仔在某論壇看到有人之需求欲取得某特定格式的資料,那該如何擷取?
該論壇已經有解答,不過包仔想說應該有不一樣的答案,所以就動手試著
做看看囉!

欲擷取資料型態如下

position = 0 type = 1 token = c
position = 1 type = 2 token = +
position = 2 type = 2 token = +
position = 3 type = 4 token = 程式
position = 7 type = 4 token = 語言

這時候包仔用下列正規式將欲擷取資料放入標籤中

(?is)(?position\s=\s\d+\stype\s=\s\d+\stoken\s=\s[\w|\/+]+)

如此即可在tag標籤中抓取每一行資料囉!

原始網頁路徑如下:
http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=3337199&SiteID=14
... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年5月12日 星期一

ObjectDataSource資料篩選應用小技巧

0 意見

這幾天包仔正用ObjectDataSource處理資料,不過卻遇上一個問題就是來源資料裡
面有某些特定筆數是包仔不需要用到的,且看包仔以下分解。

包仔的問題有人說在資料取出前就可以處理了阿,可是這樣子的話此方法如不只是共
用方法那倒是可以這麼做,但是偏偏該方法就是有很多地方有共用,總不會要包仔寫
了一堆類似的方法,這樣子有點不符合經濟效益,所以包仔就在資料取出後再作資料
篩選,方法如下:

IEnumerable Ienum = this.ObjectDataSource1.Select();

資料再次取出時,就可以對資料集合作篩選動作,以下是包仔針對DropDownList這
元件做資料重新載入的動作。

foreach (object newItem in Ienum)
{
//包仔在此處取出不需要的資料筆數索引
.
.//略
.
DropDownList1.Items.RemoveAt(接著將索引塞入);
}

this.DropDownList1.ClearSelection();
this.DropDownList1.Items[0].Selected = true;

以上就可以將資料篩選後放入欲顯示之元件囉。
... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年4月28日 星期一

簡易的防止按鈕被重覆點擊

0 意見

包仔最近在某論壇有看見有人提出如何防止按鈕被重覆點擊的問題,剛好前些日子正好有做到這部份
且看包仔以下分解。

包仔是利用JavaScript在客戶端做簡易的防止動作。
實作頁面testButton.aspx




‧以上略
〈asp:Button id="btnCheck" runat="server" Text="按我" OnClientClick="OneCheck(); return false;"〉〈/asp:Button〉
〈asp:Label ID="lblMessage" runat="server" ForeColor="Blue"〉〈/asp:Label〉

‧中間略

〈script type="text/javascript"〉
var flag = true;
function OneCheck()
{
if(flag)
{
flag = false;
document.getElementById('〈%=lblMessage.ClientID %〉').innerHTML = "執行第一次,請耐心等候動作完成...";
return true;
}
else
{
document.getElementById('〈%=lblMessage.ClientID %〉').innerHTML = "執行第二次,請勿執行相同動作。";
document.getElementById('〈%=btnCheck.ClientID %〉').disabled = true;
}
}
〈/script〉


... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年4月14日 星期一

以正規式擷取特定字串

0 意見

正規式真是個好物阿!!....用於擷取某些特定字串還真是不錯的解法,
包仔就介紹目前所應用的正規式寫法,且看分曉!!

比對格式如下:

〈A NAME="#_line_1"〉第1行
〈A NAME="#_line_2"〉第2行
〈A NAME="#_line_3"〉第3行
〈A NAME="#_line_4"〉第4行
〈A NAME="#_line_5"〉第5行
〈A NAME="#_line_6"〉第6行
〈A NAME="#_line_7"〉第7行
〈A NAME="#_line_8"〉第8行
〈A NAME="#_line_9"〉第9行

所用到的正規式如下:

(?i)[〈](?〈tag〉\w+)\s[^〉]*?(?〈name〉NAME)=['\"](?〈url〉.*?)[\"'].*?[〉](?〈Con〉.*)[^〈]

解出的字串集合為
[tag]
[name]
[url]
[Con]

取出方法如下:

foreach (Match m in Regex.Matches(Contents, "(?i)[〈](?〈tag〉\\w+)\s[^〉]*?(?〈name〉NAME)=['\\"](?〈url〉.*?)[\\"'].*?[〉](?〈Con〉.*)[^〈]"))
{
m.Groups["tag"].ToString()
m.Groups["name"].ToString()
m.Groups["url"].ToString()
m.Groups["Con"].ToString()
}


依序將以上標籤逐一取出即可。
... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年3月26日 星期三

實作DataList分頁功能

0 意見

最近友人詢問DataGrid有內建的分頁功能,可是DataList卻沒有,那該怎麼做呢?
以下且看包仔分解。

DataListTest.aspx

〈%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListTest.aspx.cs" Inherits="DataListTest" %〉

〈!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"〉

〈html xmlns="http://www.w3.org/1999/xhtml" 〉
〈head runat="server"〉
〈title〉未命名頁面〈/title〉
〈/head〉
〈body〉
〈form id="form1" runat="server"〉
〈div〉
〈asp:DataList ID="DataList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal"〉
〈ItemTemplate〉
〈asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='〈%# GetImg(Eval("AdPicName","{0}")) %〉' /〉
〈/ItemTemplate〉
〈FooterTemplate〉
〈/FooterTemplate〉
〈/asp:DataList〉〈/div〉
〈/form〉
〈/body〉
〈/html〉

DataListTest.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class DataListTest : System.Web.UI.Page
{
PagedDataSource pds;
int pageCount = 5;//取得顯示資料來源中所有項目所需的頁面總數
int pagecount = 5;
int CurrentPage = 0;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetMediaList();
}
}

//綁入圖片連結
public string GetImg(string filename)
{
return "~/DataListImages/" + filename;
}

public void GetMediaList()
{
SqlConnection cn = new SqlConnection(@"server=localhost;uid=sa;pwd=2u031j4xji4j04njo4;database=testDB");

SqlDataAdapter da = new SqlDataAdapter("select * from EIP_AdPic", cn);

DataSet ds = new DataSet();

cn.Open();

da.Fill(ds);

cn.Close();

pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = pagecount;

this.DataList1.ItemDataBound += new DataListItemEventHandler(list_ItemDataBound);

try
{
if (Request.QueryString["page"] != null)
CurrentPage = Convert.ToInt32(Request.QueryString["page"]); // int CurrentPage = 0;
else
CurrentPage = 1;

pds.CurrentPageIndex = CurrentPage - 1; // PagedDataSource.CurentPageIndex: 取得或設定目前介面的索引
}
catch
{}
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
}

void list_ItemDataBound(object sender, DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Footer)
{
if (pds.PageCount > 1) // PagedDataSource.PageCount:取得顯示資料來源中所有項目所需的頁面總數(1網頁頁尾顯示幾張分頁數)
{
string mypath = Request.Path;
int first;
int last;

if ((CurrentPage%pageCount)!= 0)
{
first = ((CurrentPage/pageCount)*pageCount)+1;
if (((CurrentPage/(pageCount)*pageCount)+pageCount)〈=pds.PageCount)
{
last=((CurrentPage/pageCount)*pageCount)+pageCount;
}
else
{
last = pds.PageCount;
}
}
else
{
first = ((CurrentPage/(pageCount+1))*pageCount)+1;
if ((((CurrentPage/(pageCount + 1))*pageCount)+pageCount)〈=pds.PageCount)
{
last = ((CurrentPage/(pageCount+1))*pageCount)+pageCount;
}
else
{
last = pds.PageCount;
}
}

e.Item.Controls.Add(new LiteralControl("〈table width='100%' height='50px'〉〈tr〉〈td height='50%'〉〈/td〉〈/tr〉" +"〈tr align='center'〉〈td Class='p1'〉"));
e.Item.Controls.Add(new LiteralControl(string.Format("目前在{0}頁", CurrentPage)));
e.Item.Controls.Add(new LiteralControl(" "));

if (!pds.IsFirstPage)
{
HyperLink newLink = new HyperLink();
newLink.ID = "firstpage";
newLink.Text = "上一頁";
newLink.NavigateUrl = string.Format("{0}?page={3}", mypath, (CurrentPage - 1).ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}

for (int i = first; i 〈= last; i++)
{
HyperLink newLink = new HyperLink();
newLink.ID = string.Format("page0{0}", i.ToString());
newLink.Text = i.ToString();
newLink.NavigateUrl = string.Format("{0}?page={3}", mypath, i.ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}

if (!pds.IsLastPage)
{
HyperLink newLink = new HyperLink();
newLink.ID = "lastpage";
newLink.Text = "下一頁";
newLink.NavigateUrl = string.Format("{0}?page={3}", mypath, (CurrentPage + 1).ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
e.Item.Controls.Add(new LiteralControl(string.Format("共{0}頁", pds.PageCount)));
e.Item.Controls.Add(new LiteralControl("〈/td〉〈/tr〉〈/table〉"));
((HyperLink)e.Item.FindControl(string.Format("page0{0}", CurrentPage))).ForeColor = Color.Black;
}
}
}
}




... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年3月10日 星期一

網頁超連結路徑置換

0 意見

包仔最近正好在玩網頁串流,其中有一個步驟剛好要判別站台內之超連結,而這些超連結有不同的寫法,有些是絕對路徑有些則是相對路徑,然而如何置換這些路徑且看包仔之分解。

transPath.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text.Trim() != "")
{
string baseURL = "http://www.test.com.tw";
//string FullPath = "http://www.test.com.tw/ch/tset.aspx";

Response.Write(TransLink(baseURL,this.TextBox1.Text.Trim(),this.TextBox2.Text.Trim()));
}
}

private string TransLink(string baseURL, string LinkPath, string FullPath)
{
string AfterTransPath = "";

if (FullPath != "")
{
int PathIndex = FullPath.LastIndexOf("/");
string LastPath = FullPath.Substring(PathIndex + 1);
string RootPath = FullPath.Replace(LastPath, "");

AfterTransPath = RootPath + LinkPath;
}
else
{
if (LinkPath.Substring(0, 1) == "/")//判斷為內部網頁則加入完整網址
{
LinkPath = baseURL + LinkPath;
}
else if (LinkPath.StartsWith("../"))//判斷為內部網頁則加入完整網址
{
//LinkPath = Regex.Match(LinkPath, "(?i)(?/.+)").Groups["tag"].ToString();

LinkPath = Regex.Split(LinkPath, "(?i)(?(^[\\.\\/]+\\/))").GetValue(3).ToString();

LinkPath = baseURL + "/" + LinkPath;
}
else if (LinkPath.IndexOf(":") < linkpath =" Regex.Match(LinkPath,">http://.+/)").Length.ToString();
}

AfterTransPath = LinkPath;
}

return AfterTransPath;
}
}

此方法可配合網頁串流遞迴掃描進而抓取整個站台的超連結


... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年3月4日 星期二

重新導向之網頁回應碼擷取

0 意見

包仔這幾天正好玩到網頁回應碼的部份,在ASP.NET中可以用HttpWebRequest、HttpWebResponse來達到,可是目前包仔就遇到一個特殊的問題,包仔在一網頁中欲取得該網頁之回應碼時,正巧此頁面是轉頁重新導向至他處,所以包仔再怎麼取都只是取到導向後頁面的內容。
在包仔百思不得其解時,在MSDN上面剛好找到說明,且看包仔分解:

transPage.aspx.cs(重新導向網頁)
CatchHttpCode.aspx.cs(實作網頁)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public partial class CatchHttpCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(CodeCatch("http://localhost/Mytest/transPage.aspx"));
}
private String CodeCatch(string URL)
{
HttpWebRequest regPage = (HttpWebRequest)WebRequest.Create(URL);

try
{
regPage.AllowAutoRedirect = false;//設定此要求不需跟隨導向網址重新要求

HttpWebResponse rspPage = (HttpWebResponse)regPage.GetResponse();
string catchCode = rspPage.StatusCode.GetHashCode().ToString();

rspPage.Close();
return catchCode;
}
catch (WebException e)
{
return e.Message + ",FAILED";
}
}
}


如此作法便可取得重新導向前的回應碼囉!!
... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo
2008年3月1日 星期六

樣板再度更新

0 意見

今天早上包仔一打開Blog就覺得怎麼看起來怪怪的,結果定眼一看哇咧!!....頂端的Banner整個不見了,之前舊的樣板不知道丟哪去了,想了半天只好在重編一次,這次包仔學聰明了先把編好的樣板備份,免的再次遺憾。

PS:身為資訊人的包仔居然犯這種忘記備份的錯誤,真是夠了。>"<... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo

XML應用--實作圖片選擇功能

5 意見

今天包仔介紹用XML檔存取方式實作一個簡易的圖片選擇。
首先製作一份用來存取的XML檔案,結構如下:

PicDetail.xml

接著製作一ASPX網頁如下:
XmlTest.aspx

〈div〉
〈table border="1" cellpadding="0" cellspacing="0" style="width: 512px; height: 100px"〉
〈tr〉
〈td style="width: 55px; height: 5px;" align="right" valign="middle"〉
〈asp:Label ID="Label1" runat="server" Text="選擇圖片" Width="78px"〉
〈/asp:Label〉
〈/td〉
〈td style="width: 313px; height: 5px"〉
〈asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"〉 〈/asp:DropDownList〉
〈/td〉
〈/tr〉
〈tr〉
〈td style="height: 48px;" align="center" colspan="2" valign="middle"〉
〈asp:Image ID="Image1" runat="server" /〉
〈/td〉
〈/tr〉
〈/table〉
〈/div〉
〈asp:XmlDataSource ID="XmlDataSource1" runat="server"〉
〈/asp:XmlDataSource〉

XmlTest.aspx.cs

using System;using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;

public partial class XmlTest : System.Web.UI.Page
{
const string MenuXml = "PicDetail.xml";
XPathDocument XmlD;
XPathNodeIterator iTerator;
XPathNavigator Nav;
XPathExpression expr;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MenuReader();
}
}

private void MenuReader()
{
this.DropDownList1.Items.Clear();
XmlD = new XPathDocument(Server.MapPath("~/"+ MenuXml));
Nav = XmlD.CreateNavigator();
expr = Nav.Compile("/Pics/Pic[@Text]");
iTerator = Nav.Select(expr);
ShowNodeDetail(iTerator);
}

//抓取節點屬性
void ShowNodeDetail(XPathNodeIterator iterators)
{
this.DropDownList1.Items.Clear();

try
{
while (iterators.MoveNext())
{
XPathNavigator nav2 = iterators.Current.Clone();
this.DropDownList1.Items.Add(new ListItem(nav2.GetAttribute("Text", ""), nav2.GetAttribute("ImgUrl","")));
}

this.DropDownList1.Items.Insert(0, new ListItem("--請選擇--", ""));
}
catch (Exception ex)
{ throw ex; }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList1.SelectedValue != "")
{
string ImgUrl = this.DropDownList1.SelectedValue;
this.Image1.ImageUrl = "~/images/" + ImgUrl;
}
}
}

操作畫面如下:

... Read more.

HEMiDEMi Technorati Del.icio.us MyShare個人書籤 Yahoo