Thursday, October 25, 2007 - Posts

KB-From Form1 to form1

古早之前寫了一個做表單欄位檢核的JS檔,裡面的第一項工作是先取得Form物件,指定給區域變數:

var defaultFormName = "Form1";
var theForm = document.forms[defaultFormName];

因為VS.NET 2003所建立的ASPX,裡面只會有一個HTML Form,名稱一律預設為Form1,在大部分的情形下,這段Code跟ASPX結合得很好。

後來手上的專案開發多半是將原有ASP.NET 1.1直接升級成ASP.NET 2.0繼續發展,用起來也一直相安無事,直到有同事建了ASP.NET 2.0的新專案,引用這個JS時卻冒出了找不到物件的Error。細究之下,發現了一個天大的祕密...

VS.NET 2003 新增ASPX的表單名稱為Form1
VS 2005新增ASPX的表單名稱為form1

雖然這個不知意義為何的改革讓人很無奈,幸好改起來不算麻煩,如下:

var defaultFormName = "form1";
if (!document.forms[defaultFormName]) defaultFormName="Form1";
var theForm = document.forms[defaultFormName];

PS: form1改在Form1前面是因應未來form1出現的比例會愈來愈高。

Update @ 2007-10-26
剛才又發現一個祕密,document.all("form1")不分大小寫,不論Form1, form1都可以被找到(注意: all()是IE Only),而document.forms[]則遵循Javascript的傳統,會區分大小寫。

XML Notepad Improvement

XML Notepad is good but it just doesn't fit my need...

I wrote a workflow system and it stores the form context and flow context in XML format.  While trouble-shooting, I often need to query context data and analyze the XML string.  When I use SQL Server Management Studio or my customize debugger to fetch the context data, what I get is a XML string.  But XML Notepad only accepts XML files, so I have to press ctrl-C to copy the string, open notepad, paste the XML string, save it as a file and open it  from XMLNotepad, quite a long road. 

I have seen another tool, XMLSpy, it can accept XML string from clipboard and parse it immediately.  Yeah, that's what I need, but XMLSpy is quite expensive.  In the other way, XMLSpy is too rich for me, what I really need is a XML Notepad which can import XML string from clipboard directly.

Thanks God, XML Notepad is open source.  I downloaded the source code from CodePlex and added two menu items, wrote a couple of  code, my dream XML Notepad is coming...

private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
    this.xmlTreeView1.CancelEdit();
    //Save the text form clipboard as file
    string rawXml = Clipboard.GetText();
    //Validate the XML data
    XmlDocument xd = new XmlDocument();
    try
    {
        //Remove <?xml> declaration
        if (rawXml.StartsWith("<?xml version"))
            rawXml =
                System.Text.RegularExpressions.Regex.Replace(
                rawXml, "[<][?]xml.+?[?]>", "");
        if (!rawXml.StartsWith("<"))
            rawXml = rawXml.Substring(rawXml.IndexOf("<"));
        //Use LoadXml to test if the XML is valid
        xd.LoadXml(rawXml);
        //Save it as a temp file
        string fileName = System.IO.Path.GetTempFileName() + ".xml";
        File.WriteAllText(fileName, Clipboard.GetText());
        //Call internal method to open the file
        InternalOpen(fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Invliad XML document.");
    }
}
 
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
    //Commit changes
    this.xmlTreeView1.Commit();
    //Prepare the XmlTextWriter
    MemoryStream ms = new MemoryStream();
    XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
    xtw.Formatting = Formatting.Indented;
    //Dump XML to the XmlTextWriter
    this.model.Document.Save(xtw);
    xtw.Flush();
    xtw.Close();
    //Send it to the clipboard
    Clipboard.SetText(Encoding.UTF8.GetString(ms.ToArray()));
}

 

Search

Go

<October 2007>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
 
RSS
【工商服務】


BlogLook Score and Rank

Syndication