需求是這樣的,當我們利用/ReportServer方式檢視Reporting Service報表,網頁上會自動產生輸入參數的TextBox WebControl,說穿了就是一個個<input type="text">,如果其中有些參數具有機密性,希望能以<input type="password">方式呈現,Reporting Service目前是無法支援的。
Reporting Service提供了兩種客製化報表檢視網頁的方法,第一種是用rs:Stylesheet Query String指定CSS檔,原本我找到了用body { background=url("BLOCKED SCRIPT...") }的做法,很不幸地,在IE7上,CSS裡內嵌的Javascript似乎會被忽略(應是基於安全的考量吧),所以只好轉向使用第二種做法,修改C:\Program Files\Microsoft SQL Server\MSSQL.2\Reporting Services\ReportServer\Pages\ReportViewer.aspx。
我的構想是這樣的,我們以參數輸入提示文字中夾帶了(*)字樣作為信物,例如: "請輸入密碼(*)",在ReportViewer.aspx的最後方加入以下的Javascript Code,就可以為這些需要隱藏輸入內容的欄位新增一個<input type="password">的密碼輸入方格,在onblur時,將值填入真正的<input type="text">中。而為了避免真正的text input洩漏天機,可利用inp.style.postion="absolute"; inp.style.left="-1000px";的技巧把它藏到火星去。最後,由於password input沒有記憶功能,在Post-Back後會自動清空,所以我們要記得在每次Post-Back時將text input中的值抄回password input,如此,就大功告成了!
</form>
<script>
var tds=document.getElementsByTagName("TD");
for (var i=0; i<tds.length; i++) {
var td=tds[i];
if (td.className=="ParamLabelCell" && td.innerText.indexOf("(*)")>0)
{
td.innerText=td.innerText.replace("(*)","");
var inpSpan=td.nextSibling.children[0];
var origInp=inpSpan.children[0];
var pwdInp=document.createElement("INPUT");
inpSpan.appendChild(pwdInp);
pwdInp=inpSpan.children[1];
pwdInp.outerHTML="<INPUT type='password' onblur='document.all(\""
+ origInp.id +"\").value=this.value;' value='" + origInp.value + "' />";
origInp.style.position="absolute";
origInp.style.left="-1000px";
}
}
</script>