2012-05-02 19 views
-2

試圖尋找在JavaScript中ASP.Net控制的標識,但它說,ID沒有找到,我看着不同的其他相關問題,但他們都在一個ASP形式或相似的,但是在這裏我使用DIV標籤,並在網頁上的給我的錯誤,ASP鍵ID沒有被發現「findbyelementID」在JavaScript

代碼:

<asp:Content ID="Content4" ContentPlaceHolderID="cphSubmit" runat="server"> 
<div id="divConfMessage" style="BORDER-RIGHT:black thin solid; BORDER-TOP:black thin solid; DISPLAY:none; Z-INDEX:200; BORDER-LEFT:black thin solid; BORDER-BOTTOM:black thin solid; background-color:white;"> 
    <br /> 
    <br /> 
    <div style="BACKGROUND-COLOR: white;TEXT-ALIGN: center" id="confirmText"></div> 
    <div style="Z-INDEX: 105;HEIGHT: 22%;BACKGROUND-COLOR: white;TEXT-ALIGN: center"></div> 
    <div style="Z-INDEX: 105;BACKGROUND-COLOR: white;TEXT-ALIGN: center"> 
     <asp:Button ID="btnConfOK" Width="200px" Height="25px" CssClass="gradientbutton" OnClick="btDelete_Click" Runat="server" Text="Yes"></asp:Button> 
     <asp:Button ID="btnConfCancel" Width="200px" Height="25px" CssClass="gradientbutton" Runat="server" Text="No"></asp:Button> 
    </div> 
</div> 
<script type="text/javascript" src="/_layouts/1033/jquery.js"></script> 
<script type="text/javascript" language="JavaScript" src="CustomDialog.js"></script> 
<script type="text/javascript" language="JavaScript"> 
     function ShowMessage() 
     { 
      DisplayConfirmMessage('Do you really want to delete this decision?',480,120); 
      document.getElementById('<%=btnConfOK.ClientID%>').focus(); 
      //SetDefaultButton('btnConfOK'); 
      return false; 
     } 
</script> 
    <asp:Button ID="btDelete" runat="server" CausesValidation="False" CssClass="gradientbutton" 
    UseSubmitBehavior="False" 
    OnClientClick="this.disabled=true;this.value='Please Wait...';ShowMessage();" 
    Text="Delete" Width="200px" /> 

編輯:

我做了更改,然後對話框出現並消失:|,我想我需要將控件添加到DOM,但不知道如何在此上下文中執行此操作:

這裏的教程鏈接我也跟着 Dialog box tutorial

js腳本

var divWidth = ''; 
var divHeight = ''; 
var txtFirstButton = 'OK'; 
var txtSecondButton = 'Cancel' 
    function DisplayConfirmMessage(msg,width,height) 
    { 
      // Set default dialogbox width if null 
      if(width == null) 
      divWidth = 180 
      else 
      divWidth = width; 

      // Set default dialogBox height if null 
      if(height == null) 
      divHeight = 90 
      else 
      divHeight = height; 


      // Ge the dialogbox object 
      var divLayer = document.getElementById('divConfMessage'); 
      // Set dialogbox height and width 
      SetHeightWidth(divLayer) 
      // Set dialogbox top and left 
      SetTopLeft(divLayer); 

      // Show the div layer 
      divLayer.style.display = 'block'; 
      // Change the location and reset the width and height if window is resized 
      window.onresize = function() { if(divLayer.style.display == 'block'){ SetTopLeft(divLayer); SetHeightWidth(divLayer)}} 
      // Set the dialogbox display message 
      document.getElementById('confirmText').innerHTML = msg; 
    } 

    function SetTopLeft(divLayer) 
    { 
     // Get the dialogbox height 
     var divHeightPer = divLayer.style.height.split('px')[0]; 

     // Set the top variable 
     var top = (parseInt(document.body.offsetHeight)/ 2) - (divHeightPer/2) 
     // Get the dialog box width 
     var divWidthPix = divLayer.style.width.split('px')[0]; 

     // Get the left variable 
     var left = (parseInt(document.body.offsetWidth)/2) - (parseInt(divWidthPix)/2); 
     // set the dialogbox position to abosulute 
     divLayer.style.position = 'absolute'; 

     // Set the div top to the height 
     divLayer.style.top = top 

     // Set the div Left to the height 
     divLayer.style.left = left; 
    } 
    function SetHeightWidth(divLayer) 
    { 
     // Set the dialogbox width 
     divLayer.style.width = divWidth + 'px'; 
     // Set the dialogbox Height 
     divLayer.style.height = divHeight + 'px' 
    } 

    function SetDefaultButton(defaultButton) 
    { 
      // Set the focus on the Cancel button 
      document.getElementById(defaultButton).focus(); 
    } 

如果我刪除 「UseSubmitBehavior =」 假」,它工作正常,當我點擊是的,它不只是't關閉對話框

+0

跟蹤它,看看它的id試圖調用 – tehdoommarine

+1

說不上來爲什麼我得到了它的負面評論:/ – TimeToThine

回答

1

您必須將輸出放在引號中:

document.getElementById(<%=btnConfOK.ClientID%>) 

應該

document.getElementById("<%=btnConfOK.ClientID%>") 
+0

抱歉,但它並沒有幫助:/要麼以及 – TimeToThine

4

的getElementById需要一個字符串,所以你需要引用的ID,像這樣:

document.getElementById('<%=btnConfOK.ClientID%>').focus(); 
+0

抱歉,但它並沒有幫助:/ – TimeToThine

+0

那麼你原始代碼有幾個錯誤。你可以將你的問題修剪成最簡單的方法嗎? – bmm6o

+0

@TimeToThine沒有理由對試圖幫助你的人冒犯。你發佈了大量的代碼,並且有多個人告訴你**你發佈的代碼錯誤**。頁面其他地方的JavaScript錯誤可能會導致您的問題,因此要求簡化您的代碼並解決問題並非荒謬的要求。 –

1

你缺少語法document.getElementById("<%=btnConfOK.ClientID%>").focus();

瞭解更多關於document.getElementById

編輯

function ShowMessage() 
     { 
      DisplayConfirmMessage('Do you really want to delete this decision?',480,120); 
      document.getElementById("<%=btnConfOK.ClientID%>").focus(); 
      //SetDefaultButton('btnConfOK'); 
      return false; 
     } 

如果你刪除按鈕做後回,上刪除按鈕點擊使用event.preventDefault

+0

對不起,但它沒有幫助:/或 – TimeToThine

+0

@TimeToThine:你有沒有改變你的**函數ShowMessage()**以上格式? –

+0

是的,我做了,但它出現並消失,然後 – TimeToThine