-2

JavaScript で ASP.Net コントロールの Id を見つけようとしていますが、ID が見つからないというメッセージが表示されます。別の関連する質問を調べましたが、それらはすべて ASP フォームまたは類似のものですが、ここでは DIV タグを使用しており、エラーが発生していますページ上で、

コード :

<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 に追加する必要があると思いますが、このコンテキストでそれを行う方法がわかりません :|

ここに私がたどったチュートリアルリンクがあり ます ダイアログボックスのチュートリアル

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="False" を削除すると、正常に動作しますが、[はい] をクリックしてもダイアログ ボックスが閉じません。

4

3 に答える 3

4

getElementById は文字列を受け取るため、次のように ID を引用する必要があります。

document.getElementById('<%=btnConfOK.ClientID%>').focus();
于 2012-05-02T15:18:23.583 に答える
1

出力は引用符で囲む必要があります。

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

する必要があります

document.getElementById("<%=btnConfOK.ClientID%>")
于 2012-05-02T15:17:40.957 に答える