我正在ASP中使用JavaScript代碼在頭部的網站上工作。 我想實現我自己的上下文菜單,所以我在本節中爲它編寫了邏輯。我的代碼如下:ASP.Net Javascript代碼拋出奇怪的異常(即使當評論!)
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
lastFileId = "<%# fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID").ToString() %>";
}
正如你所看到的,我還要呼籲的背景下,這是用來獲取所選行的ID一些C#代碼。下面的代碼拋出一個異常,也就是如果FocusedRowIndex沒有初始化可以預料:
出於測試目的,我再決定要不要使用FocusedRowIndex了,但總是獲得第一行:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
lastFileId = "<%# fileGridGrouping.GetRowValues(0, "ID").ToString() %>";
}
此代碼拋出也不例外,這也證明了這個問題從FocusedRowIndex未初始化(因此設置爲-1)來了。夠了!我決定很容易解決這個問題:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
if ("<%# fileGridGrouping.FocusedRowIndex %>" <= 0) return;
lastFileId = "<%# fileGridGrouping.GetRowValues(0, "ID").ToString() %>";
}
但是,出人意料的是,相同的異常被拋出,在準確的同一地點。 作爲最後一個試圖瞭解究竟是什麼發生,我決定發表評論的罪魁禍首:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
if ("<%# fileGridGrouping.FocusedRowIndex %>" <= 0) return;
//lastFileId = "<%# fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID").ToString() %>";
}
而現在這只是徹頭徹尾的怪異:
的註釋行拋出一個異常,就好像評論代碼實際上由網站運行一樣。我嘗試重新啓動Visual Studio,重新構建一切從頭開始,但同樣的事情一次又一次地發生。
若要添加到奇怪的地方,所有這些代碼運行之前,實際發生任何點擊,但當頁面加載。函數fileGridGrouping_ContextMenu()應該在頁面上的表中右鍵單擊時調用,但是在實際發生任何事情之前運行此代碼。我猜猜這裏有某種彙編或類似的事情發生?我不知道怎麼回事,也不知道如何解決。
編輯:我現在通過更改大部分代碼來解決問題。原來我沒有用最好的功能來做我想做的事。哎呀。感謝所有幫助過我的人! 這裏是工作代碼:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
fileGridGrouping.GetRowValues(fileGridGrouping.GetFocusedRowIndex(), "ID", OnGetRowValues);
}
function OnGetRowValues(value) {
lastFileId = value;
}
爲了更好地understandig:你寫的功能是在C#或JavaScript的? – reporter 2014-10-20 08:53:47
這些函數是用Javascript編寫的,但是在引號中調用一些C#代碼。 – pankee 2014-10-20 09:01:51
只需清除瀏覽器緩存並重新加載即可。可能是它從Cache中取得的。 – Jankya 2014-10-20 09:08:42