JavaScript无效(自定义对话框)

时间:2012-05-02 10:13:06

标签: c# javascript jquery asp.net .net

遵循本教程,

Custom DialogBox

我已经为ASP.Net页面提供了3个asp内容占位符,他们已经在使用某种类型的脚本了,这不是问题。

问题是,我能够使用“OnClientClick”属性调用函数,但此函数在不同的.js文件中具有更多函数。如果我在调用其他函数之前发出警报它可以正常工作,但在它完成我的第一个功能之后就没有了。

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

<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(..)为null或不是对象

2 个答案:

答案 0 :(得分:1)

我想这个

document.getElementById('btnConfOK') 

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

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

答案 1 :(得分:0)

也许有些人,为什么src被添加到网址没有'/'...

更改此行:

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

要:

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

如果这对代码无效,或者您将其放在另一个位置0_0