1

按照本教程,

自定义对话框

我已经有 3 个用于 ASP.Net 页面的 asp 内容占位符,而且我猜他们已经在使用某种脚本,这在这里没有问题。

问题是,我可以使用“OnClientClick”属性调用一个函数,但是这个函数在不同的 .js 文件中有更多函数。如果我在它调用其他函数之前发出警报,它会起作用,但在它执行我的第一个函数之后不会。

教程列出了所有 javascript 代码,这是我在 ASP.Net Content 地方使用的代码,以使其工作,

<asp:Content ID="Content4" ContentPlaceHolderID="cphSubmit" runat="server">
 <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()
        {
            alert("1");
            SetText('Yes','No');
            alert("2");
            DisplayConfirmMessage('are you sure',180,90)
            SetDefaultButton('btnConfOK');
            return false;
        }
 </script>
            <div id="divConfMessage" runat="server" 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">
            <div style="BACKGROUND-COLOR: #6699cc;TEXT-ALIGN: center" id="confirmText">
            </div>
            <div style="Z-INDEX: 105;HEIGHT: 2%;BACKGROUND-COLOR: white;TEXT-ALIGN: center">
            </div>
            <div style="Z-INDEX: 105;BACKGROUND-COLOR: white;TEXT-ALIGN: center">
                <asp:Button ID="btnConfOK" Runat="server" Text="OK"></asp:Button>
                <asp:Button ID="btnConfCancel" Runat="server" Text="Cancel"></asp:Button>
            </div>
        </div>
<hr />


    <asp:Button ID="btDelete" runat="server" 
    OnClientClick="ShowMessage();"
    Text="Delete" UseSubmitBehavior="False" Width="200px"  />

我暂时删除了一些其他控件,它显示警报“1”但没有达到“2”,

这是 CustomDialog 脚本,

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').innerText = 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 SetText(txtButton1,txtButton2)
    {
            // Set display text for the two buttons
            if(txtButton1 == null)
            document.getElementById('btnConfOK').innerText = txtFirstButton;
            else
            document.getElementById('btnConfOK').innerText = txtButton1;

                            // Set display text for the two buttons
            if(txtButton2 == null)
            document.getElementById('btnConfCancel').innerText = txtSecondButton;
            else
            document.getElementById('btnConfCancel').innerText = txtButton2;

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

刚刚注意到它达到了这个功能

function SetText(txtButton1,txtButton2)
    {
            alert("2");
            // Set display text for the two buttons

然后它迷路了,它试图从 txtButton1 获取价值,这可能意味着,我的 ASPX 页面中的 Div 标签没有正确呈现:(

                if(txtButton1 == null)
            document.getElementById('btnConfOK').innerText = txtFirstButton;
            else
            document.getElementById('btnConfOK').innerText = txtButton1;

            alert("3");

* *编辑*如果我删除 SetText 函数,它会显示,但不会触发我的“OnClick”方法,只是刷新页面***

        <asp:Button ID="Button1" runat="server" CausesValidation="False" CssClass="gradientbutton"
    OnClick="btDelete_Click" OnClientClick="ShowMessage();this.disabled=true;this.value='Wait...';"
    Text="Delete" UseSubmitBehavior="False" Width="200px"  />

刚刚意识到页面上出现错误,即使它显示确认框

说 findElementbyid(..) 为空或不是对象

4

2 回答 2

1

我认为这

document.getElementById('btnConfOK') 

应该使用 ClientID 而不是实际 ID,因为它是一个 .NET 控件,ID 将被重写。所以应该是

document.getElementById('<%=btnConfOK.ClientID%>')
于 2012-05-02T10:44:43.190 回答
0

也许不知何故,添加的 src 没有 '/' 到 url ......

更改此行:

<script type="text/javascript" language="JavaScript" src="CustomDialog.js"></script>

至:

<script type="text/javascript" language="JavaScript" src="./CustomDialog.js"></script>

如果这不起作用,则代码有问题,或者您已将其放置在另一个位置 0_0

于 2012-05-02T10:19:25.003 回答