-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%>").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 。

于 2012-05-02T15:19:59.053 回答
1

您必须将输出放在引号中:

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

应该

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