按照本教程,
我已经有 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(..) 为空或不是对象