即使存在于aspx代码中,span元素在javascript函数中也始终为null

时间:2016-04-13 13:24:55

标签: javascript asp.net webforms getelementbyid

在VS-2012 Express for Web,WebForms,Ajax-VB.Net中进行编码。

我希望在按钮单击启动长服务器进程时阻止用户离开页面。第一次单击“cmdDoMonthlyReports”按钮并完成该过程以在[span]元素中显示结果时,这非常有效。但是,如果我第二次单击该按钮,则不会从页面中清除[span] innerHTML - 但是流程再次运行(如预期的那样),并且在流程执行期间显示在[span]中的旧结果 - 这是不正确。

问题是当运行'SetIsBusy()'js-function时不会清除[span],因为元素oSpan始终为null

var oSpan = document.getElementById('< %= idSpanResult.ClientID%>'); 

返回null,因此innerHTML不会被清除。

我的问题是“为什么”oSpan元素总是为空?

非常感谢任何评论或解决方案......谢谢。

这是我对asp.button的标记。

<asp:Button ID="cmdDoMonthlyReports" runat="server" Text="Do Monthly Reports"  SkinID="cmdButton" OnClientClick="SetIsBusy(true)"/>

这是ClientClick的javascript - 调用'SetIsBusy(true)'...

var m_bIsBusy = false;
window.onbeforeunload = confirmExit;
function confirmExit() {
   var oCtrl = document.getElementById('<%= idIsBusy.ClientID%>');
   //if (oCtrl != null) { m_bIsBusy = (oCtrl.value.toLowerCase() === "true") } else { m_bIsBusy = false; }
   m_bIsBusy = (oCtrl != null) ? (oCtrl.value.toLowerCase() === "true") : false;
   if (m_bIsBusy) {
      return "You have attempted to leave this page.  If you leave this page, now, you will lose all of your generated monthly reports.  Are you sure you want to leave this page?";
   } 
}

function SetIsBusy(p_IsBusy) {
   m_bIsBusy = p_IsBusy;
   if (m_bIsBusy == true) {
     var oSpan = document.getElementById('< %= idSpanResult.ClientID%>');
     if (oSpan == null) { oSpan = document.getElementById('idSpanResult'); }
     if (oSpan != null) { oSpan.innerHTML = ''; }
  }
}

button.click还会在代码隐藏中的服务器上触发,在那里进行一些验证,然后执行长时间运行的进程。该过程的结果包含在文本字符串中,该字符串将响应更新回浏览器。这是代码隐藏代码。

Protected Sub cmdDoMonthlyReports_Click(sender As Object, e As EventArgs) Handles cmdDoMonthlyReports.Click
     ...some validation code...
     Call sbGenerateReports()

     Dim sResultText As String = m_sResultsHTML

     idSpanResult.InnerHtml = sResultText
     idSpanResult.Visible = True

     updpnlDetails.Update()

  End Sub

0 个答案:

没有答案