2007年10月29日 星期一

.NET 2.0 RadioButtonList access on the client side using Javascript

The asp:RadioButtonList control will be rendered as a series of input HTML controls wrapped in a span on the client side. To access each element of the RadioButtonList on the client side, use the following codes, whearas id as the ClientID of the RadioButtonList:

var obj = document.getElementById(id);
var radios = obj.getElementsByTagName('input');

Afterwards, radios can be used as an array of radio inputs, which you can access as in normal conditions.

2007年10月14日 星期日

.NET 2.0 TextBox ReadOnly attribute

在.NET 2.0裡面,asp:TextBox的ReadOnly屬性如果被設成true的話(例如: <asp:textbox id="tbx_test" runat="server" />,或是使用tbx_test.ReadOnly = true;),會對前端的javascript造成一點影響,譬如我們常常在前端使用javascrip將textbox的值改變,但是當程式postback到後方以後,會發現textbox所保有的值仍然是舊的值,要想改正這個行為,有幾種方法:

1. 在後端以Request["tbx_test"],或是Request.Form["tbx_test"]來取得改正過後的值,然後用tbx_test.Text = Request["tbx_test"].ToString();在後重新設定textbox的值,

2. 不使用ReadOnly="true"或是tbx_test.ReadOnly = true;,而是在後端使用
tbx_test.Attributes.Add("ReadOnly", "ReadOnly");,或是
tbx_test.Attributes["ReadOnly"] = "ReadOnly";


The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code. (Quoted from MSDN)

reference: http://darkthread.blogspot.com/2006/11/kb-aspnet-20-textboxreadonly.html

2007年10月1日 星期一

.NET 2.0 asp:TextBox AutoPostBack

之前在使用asp:TextBox時,有時會基於某些原因將AutoPostBack設成true,當這樣做時,發現在本機上執行是沒有問題的(其實一開始本機上執行也不行,但是當我註冊WebForms.js這個resource到Page上以後,本機上就可以了,所以讓我以為問題已經解決,後來發現在別的電腦上還是不行...),但是當我發佈到server上,在別的電腦執行時,每按一下鍵盤,就會一直出現一個javascript錯誤:

此處需要物件(Object expected)

根據原始碼以及錯誤訊息指出的line number,發現是一段code的嫌疑最大,並且懷疑WebForm_TextBoxKeyHandler是此處所缺少的object:

<asp:TextBox.......onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$tbx_contname\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;"......

問題成因: 有聽過討論區裡有人說過是IE7.0的問題,但是我懷疑這個可能性,因為我們用的IE版本是6.0的,也有聽過是因為WebResource沒有被正確download到client端,這個我比較相信一點,我唯一確定的是,這段code是因為使用TextBox的AutoPostBack="true"屬性所造成的

如上面所述,本以為RegisterClientScriptResource可以解決問題(因為WebForm_TextBoxKeyHandler定義於其中),但是沒有,又找不到如何使.NET不要自動加這段程式碼,所以最後決定將這段code的生成原因:AutoPostBack拿掉,拿掉AutpPostBack之後,果然問題便解決了

相當奇怪的一個bug

之前在測試計畫管理系統時,發現了一個很奇怪的bug,就是在使用者於TextBox裡面輸入某些字串,並試圖以這些字串為參數帶入popup視窗時,出現了403:Access Forbidden error,error message如下:

Due to the presence of characters known to be used in Cross Site Scripting attacks, access is forbidden. This web site does not allow URls which might include embedded HTML tags.

為什麼說這個error很奇怪呢?因為當時所測試的字串,為中文字,並有經過escape()的encoding,(就我所知,一般escape是將自元轉換成%xx格式,xx為hex數字,照道理講並沒有內含任何HTML tags才對),經過反覆嘗試,確定有內含""字的字串,都會出現同樣錯誤,我一直不知這個錯誤的詳細原因為何(雖然是有懷疑是因為encoding方式的問題)

所以後來將escape置換為encodeURI後,error倒是沒有再出現了,(我只能說,真是神出鬼沒ㄚ...)

escape, encodeURI, encodeURIComponent的比較

這三個function都是javascrip中提供為encoding用的,只是三個略有不同,以下為三者的比較:

escape: 回傳一個Unicode格式的字串,所有空格,符號,以及其他non-ASCII的字元都會以%xx的格式來取代,其中xx表示與字元相對應的十六進位(base-16)數字,例如:空格space即可表示為"%20"

注意: escape並不會encode含有"+"的字串,同時也無法正確處理含有non-ASCII的字元,也因此,建議盡量避免使用escape

encodeURI: 可將一個字串encode為URI,但是無法encode某些特定字串,例如: ":", "/", ";", 和 "?"

注意: encodeURI較escape來說,是比較恰當的,因為encodeURI是將字串encode成URI,而不僅只是QueryString,要特別注意的是encodeURI並不encode "'",因這個字元在URI裡是有效的

encodeURIComponent: 與encodeURI相同,同樣會傳回一個encoded的URI,除此之外,並可以encode所有字串,包括encodeURI無法encode的字元,但是要注意的是,如果字串中包括路徑,那麼路徑中的"/"也會被encoded,因此當我們將這個路徑當作request傳給web server時,便是無效的了

注意: encodeURIComponent是比較適合大多數情況下使用的,但同樣的,encodeURIComponent也並不encode "'"

reference: http://xkr.us/articles/javascript/encode-compare/