![](/img/trans.png)
[英]JavaScript sometimes prevents server event from firing with asp.net
[英]asp.net javascript .click() event not firing on server
我有以下代碼在開發機器上正常工作。 但是,當我將其部署到IIS時, the .click()
不會觸發。
我有下拉框,當選擇狀態時,我添加以下代碼以打開RadWindow
if (ddlStatusID.SelectedValue.ToString() == "2") btnUpdateStatus.Attributes.Add("onclick", "return OpenWindow2('" + ResolveUrl("~/Request/AddBillableTime.aspx? RequestId=" + RequestId.ToString()) + "','650','320');"); else btnUpdateStatus.Attributes.Add("onclick", "");
在彈出頁面中,當用戶點擊我添加的按鈕時,請執行以下操作
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", "ClosePopup();", true);
這是ClosePopup的javascript。
function ClosePopup() {
//debugger;
alert('This is before closing the window');
var oWindow = GetRadWindow();
oWindow.argument = null;
oWindow.close();
alert('This is after closing the window');
oWindow.BrowserWindow.ButtonClick();
}
在主窗口中,我有以下javascript,它在上面的javascript的最后一行中調用。
function ButtonClick() { //debugger; alert('This is before button click!'); var btnUpdateStatus = document.getElementById('ctl00_ContentPlaceHolder1_btnUpdateStatus'); alert('This is getting the button!'); btnUpdateStatus.setAttribute("onclick", ""); alert('This is setting the button!'); btnUpdateStatus.click(); alert('This is after clicking the button!');
}
我已經使用警報來檢查javascript函數執行的程度。
當我使用Visual Studio運行它時,以及當我在本地IIS服務器中托管它時,整個功能正常工作。 但是,當我將其部署到服務器時,click()事件將停止觸發。
如果代碼中有任何錯誤,請告訴我。
當我被分配到一項新任務時,我沒有時間研究這個問題。 但是一旦我回到同一個任務中就不得不修復它。 我從我的一個朋友那里得到了一些幫助來解決這個問題。 我不得不稍微改變當前的邏輯,並解釋了下面的解決方案。
我更改了ButtonClick
函數,如下所示,使用__doPostBack
函數創建一個返回頁面的手動帖子。
function ButtonClick() {
__doPostBack('btnUpdatePage', '');
return;
}
我從頁面的Page_Load
方法處理了帖子。
if (Request["__EVENTTARGET"] == "btnUpdatePage")
{
UpdateStatus();
}
如果回發事件目標與__doPostBack
方法中指定的事件目標匹配,那么我調用了一個方法來更新狀態。
這樣我就可以避免這個問題。 但我不知道為什么事件沒有使用舊的ButtonClick
函數觸發。
如果代碼在您的PC上正常工作,那么對我來說唯一可疑的就是這兩行:
1. document.getElementById('ctl00_ContentPlaceHolder1_btnUpdateStatus');
2. btnUpdateStatus.setAttribute("onclick", "");
對於第一行,您應該這樣做,而不是:
document.getElementById('<%=btnUpdateStatus.ClientID%>');
對於第二行,似乎您將onclick
處理程序設置為""
,基本上不應該執行任何操作。
這根本不是問題。 最好的辦法是使用Firebug調試它,在正在調用的相應Javascript函數中放置一個斷點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.