ASP 按钮 ID 在 JavaScript 中找不到“findbyelementID”

ASP button ID not being find "findbyelementID" in JavaScript

本文关键字:找不到 findbyelementID JavaScript 按钮 ID ASP      更新时间:2023-09-26

试图在 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();
    }

如果我删除"使用提交行为="False",它可以正常工作,除非我单击"是",它不会关闭对话框

getElementById 需要一个字符串,所以你需要用 id 加上引号,如下所示:

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

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

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

应该是

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

您缺少语法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。