簡介:anthem 是一個很好用的 ajax 框架,支持 asp.net 1.1, 2.0。
由于該框架的所有控件都繼承自 asp.net 自身的服務器控件,保留了幾乎所有這些控件的屬性和行為(除了把它們的 postback 改為 callback 的無刷新調用之外)。所以學習曲線很平緩。
今天我在使用 anthem 的時候碰到了一個比較麻煩的調試問題,記錄于此。
在下面的代碼中,我用了一個 anthem.repeater 控件。
<asp:xmldatasource id="xmldatasource2" runat="server" xpath="http://needdocs/doc"
enablecaching="false"></asp:xmldatasource>
<table class="mytable" width="100%" cellspacing="0" cellpadding="0">
<anthem:repeater id="rptneeddocs" runat="server" datasourceid="xmldatasource2"
autoupdateaftercallback="false">
<headertemplate>
<tr class="formtitle">
<td>
選中</td>
<td>
文件、圖紙名稱</td>
<td>
應送</td>
<td>
是否原件</td>
<td>
備注</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td>
<asp:checkbox id="chkdoc" runat="server" checked="true" />
<asp:hiddenfield id="hiddocid" runat="server" value='<%# xpath("@id") %>' />
</td>
<td>
<asp:label id="lbldocname" runat="server" text='<%# xpath("@name") %>' />
</td>
<td>
<asp:textbox id="txtquantity" runat="server" text='<%# xpath("@quantity") %>' width="30" />
</td>
<td>
<asp:radiobuttonlist id="radiolist_isoriginal" runat="server" selectedvalue='<%# xpath("@isoriginal") %>'
repeatdirection="horizontal">
<asp:listitem value="true">原件</asp:listitem>
<asp:listitem value="false">副本</asp:listitem>
</asp:radiobuttonlist>
</td>
<td>
<asp:textbox id="txtcomment" runat="server" text='<%# xpath("comment") %>' />
</td>
</tr>
</itemtemplate>
<footertemplate>
</footertemplate>
</anthem:repeater>
</table>
這個代碼在運行時,有時候會出現一個 js 錯誤:“未知的運行時錯誤”。
而該錯誤只在特定情況下發生,在其他類似情況下正常。
幸虧 vs 2005 提供了非常強大的客戶端腳本調試功能。我終于將錯誤定位到了 anthem 產生的一行代碼上:
control.innerhtml = result.controls[controlid];
查了相關資料后發現,在 ie 下,對 innerhtml 屬性賦值的時候,會對所賦的值進行檢查。如果不是 well formed, 則可能會出現“未知的運行時錯誤”。
于是我判斷 anthem.repeater 輸出的 html 出了問題。從上面代碼中高亮的兩行可以看到,table 標簽在 repeater 的外面。因此 repeater 本身輸出的是一系列 tr, 并不是 well formed 的一個整體。
于是我將 table 的標簽頭尾分別放入 repeater 的 headertemplate 和 footertemplate,問題解決。
(之所以先前把 table 標簽放到外面去了,是因為放在 headertemplate 和 footertemplate 中的時候,不知道為什么 vs 的設計器不能切換到設計視圖了。而改成這樣可以解決問題。)
修改成功后的代碼如下:
<asp:xmldatasource id="xmldatasource2" runat="server" xpath="http://needdocs/doc"
enablecaching="false"></asp:xmldatasource>
<anthem:repeater id="rptneeddocs" runat="server" datasourceid="xmldatasource2" autoupdateaftercallback="false">
<headertemplate>
<table class="mytable" width="100%" cellspacing="0" cellpadding="0">
<tr class="formtitle">
<td>
選中</td>
<td>
文件、圖紙名稱</td>
<td>
應送</td>
<td>
是否原件</td>
<td>
備注</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td>
<asp:checkbox id="chkdoc" runat="server" checked="true" />
<asp:hiddenfield id="hiddocid" runat="server" value='<%# xpath("@id") %>' />
</td>
<td>
<asp:label id="lbldocname" runat="server" text='<%# xpath("@name") %>' />
</td>
<td>
<asp:textbox id="txtquantity" runat="server" text='<%# xpath("@quantity") %>' width="30" />
</td>
<td>
<asp:radiobuttonlist id="radiolist_isoriginal" runat="server" selectedvalue='<%# xpath("@isoriginal") %>'
repeatdirection="horizontal">
<asp:listitem value="true">原件</asp:listitem>
<asp:listitem value="false">副本</asp:listitem>
</asp:radiobuttonlist>
</td>
<td>
<asp:textbox id="txtcomment" runat="server" text='<%# xpath("comment") %>' />
</td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</anthem:repeater>
經過這次的調試,我覺得 ajax 除了帶來了界面上響應迅速的好處之外,因為引入大量 js,也增大了調試的難度,因此應用的時候還是要根據情況取舍。不能什么都上 ajax.
新聞熱點
疑難解答
圖片精選