就像很多网站常见的弹出层模式的登录入口
这个层初始display设为none 点击按钮的时候改为block就显示出来了 这个谁都知道可是当这个层上放了要回发引发事件的控件时 一回发 层就没了 ,咋办 求大大们教教俺用runat="server"的就算了 这个我知道 但是貌似不能用javascript控制了
这个层初始display设为none 点击按钮的时候改为block就显示出来了 这个谁都知道可是当这个层上放了要回发引发事件的控件时 一回发 层就没了 ,咋办 求大大们教教俺用runat="server"的就算了 这个我知道 但是貌似不能用javascript控制了
如果按了,则在IsPostBak事件中再显示一遍div,如下:
1.页面添加隐藏域:
<input id="Hidden1" type="hidden" runat="server" />2.div中的回发事件下设置:
Hidden1.Value = "1";3.在Page_Load事件下判断是否按了回发,若是则调用test()函数显示div:
if (IsPostBack)
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "test()", true);4.test()函数
function test()
{
document.getElementById("div1").style.display = "block";
document.getElementById("Hidden1").value = "0";
}
OnClientClick=<%# "OpenWindow('','','');return false;"%>
function OpenWindow(id,width,height)
{
var url = "a.aspx";
OpenJWindow('#openwin', url, width,height, EditClosed);
}
http://topic.csdn.net/u/20090811/23/a0812cf7-4989-408b-b73c-ad6faf458369.html
4.test()函数
function test()
{
if (document.getElementById("Hidden1").value == "1")
{
document.getElementById("div1").style.display = "block";
document.getElementById("Hidden1").value == "0";
}
}
<script language="JavaScript1.2">
isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
DoIt=false;
function ddInit(e){
TWD=isIE ? "BODY" : "HTML";
TWD1=isIE ? document.all.theLayer : document.getElementById("theLayer");
TWD2=isIE ? event.srcElement : e.target;
while (TWD2.id!="titleBar"&&TWD2.tagName!=TWD){
TWD2=isIE ? TWD2.parentElement : TWD2.parentNode;
}
if (TWD2.id=="titleBar"){
offsetx=isIE ? event.clientX : e.clientX;
offsety=isIE ? event.clientY : e.clientY;
nowX=parseInt(TWD1.style.left);
nowY=parseInt(TWD1.style.top);
ddEnabled=true;
document.onmousemove=dd;
}
}
function dd(e){
if (!ddEnabled) return;
TWD1.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx;
TWD1.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
return false;
}
function ddN4(TWD3){
if (!isN4) return;
N4=eval(TWD3);
N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
N4.onmousedown=function(e){
N4.captureEvents(Event.MOUSEMOVE);
N4x=e.x;
N4y=e.y;
}
N4.onmousemove=function(e){
if (DoIt){
N4.moveBy(e.x-N4x,e.y-N4y);
return false;
}
}
N4.onmouseup=function(){
N4.releaseEvents(Event.MOUSEMOVE);
}
}
function hideMe(){
if (isIE||isNN) TWD1.style.visibility="hidden";
else if (isN4) document.theLayer.visibility="hide";
}
function showMe(){
if (isIE||isNN) TWD1.style.visibility="visible";
else if (isN4) document.theLayer.visibility="show";
}
document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");
</script><body>
<!-- BEGIN FLOATING LAYER CODE //-->
<div id="theLayer" style="position:absolute;width:250px;left:9px;top:135px;visibility:hidden"> <table border="0" width="250" bgcolor="#990000" cellspacing="0" cellpadding="5">
<tr>
<td width="100%">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="36">
<tr>
<td id="titleBar" style="cursor:move" width="100%">
<ilayer width="100%" onSelectStart="return false">
<layer width="100%" onMouseover="DoIt=true;if (isN4) ddN4(theLayer)" onMouseout="DoIt=false">
<font face="Arial" color="#FFFFFF">Layer Title Goes Here</font>
</layer>
</ilayer>
</td>
<td style="cursor:hand" valign="top">
<a href="#" onClick="hideMe();return false"><font color=#ffffff size=2 face=arial style="text-decoration:none">X</font></a>
</td>
</tr>
<tr>
<td width="100%" bgcolor="#FFFFFF" style="padding:4px" colspan="2">
<!-- PLACE YOUR CONTENT HERE //-->
Your content goes here.<br>
You can use any html code or text.<br>
including <a href="http://www.texaswebdevelopers.com/"target="_blank">hyperlinks</a>.
<!-- END OF CONTENT AREA //-->
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!-- END FLOATING LAYER CODE //-->
To show the layer:
<a href="javascript:showMe();">show</a>
To hide the layer:
<a href="javascript:hideMe();">hide</a>
</body>
谢谢你的帮忙 ,jquery还没接触过 刚做B/S没多久 等熟悉了再去学学 呵呵
如果把最后一个参数改为false就会在页面上直接显示出"test();"字样我用了ajax的UpdatePanel控件 是不是这个有影响啊
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "test()", true);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "test()", true);
我知道用了ajax一般都是用
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('提示');", true);
这样来使用alter()方法
但是为什么用来调用已经在前台页面中写好的function就不行呢?
你js的function可能有错哦!!
<script type="text/javascript">
function test()
{
if (document.getElementById("Hidden1").value == "1")
{
document.getElementById("topcontainer").style.display = "block";
document.getElementById("Hidden1").value == "0";
}
}
</script>
<input id="Hidden1" type="hidden" runat="server" />隐藏域
<div id="topcontainer" style="margin-bottom: 3px; text-align: center; display: none"> 我要控制的层
你是否有其他的js是否有错!
有时会漏了括号或花扩号!
你也可以试验一下不考虑其他的回发,直接在IsPostBack中显示Div如:
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "document.getElementById('topcontainer').style.display = 'block';
", true);
只有test()这个函数时
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "test();", true);
这句在页面加载时还是会报“缺少对象”的错误
注释掉这句就完全没有错误了这句可以正常执行
function test()
{
alert('11');
if (document.getElementById('Hidden1').value == "1")
{
alert('22');
document.getElementById('topcontainer').style.display = "block";
document.getElementById('Hidden1').value = "0";
}
alert('33');
}用ScriptManager.RegisterStartupScript(this, this.GetType(), "", "test();", true);调用脚本
只显示了"11"
后面2个alert都没有执行
问题很明显出现在if块中 但是没看出有什么问题啊
去掉 runat="server"以后可以执行"11"和"33"
说明脚本没问题了就是这个<input id="Hidden1" type="hidden" runat="server" />
他的ID被编译以后是不是改变了 导致缺少对象
换服务端的隐藏域试试!
<asp:HiddenField ID="HiddenField1" runat="server" />
用
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "document.getElementById('Hidden1').value = '1';", true);
来改隐藏域的值alter('22')还是没有执行
难道这样改不对吗?
页面执行以后源代码可以看到
<asp:HiddenField ID="HiddenField1" runat="server" />
被翻译成了
<input type="hidden" name="ctl00$ContentPlaceHolder1$HiddenField1" id="ctl00_ContentPlaceHolder1_HiddenField1" />
他的id变了
所以documen.getElemetById就找不到他了
这种写法经常用!不会有你所述错误!
hidden可能你位置放错了!不要放到控件里面,放在最外面!!
txtCalendar.Style.Add("display", "none");
txtCalendar.Style.Add("display", "block");
<input id="Hidden1" type="hidden" runat="server" />
那我把他放在哪里?
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
function test() {
if (document.getElementById("<%=Hidden1.ClientID%>").value == "1")
......
}
</script>
</asp:Content>
这样写可以找到隐藏域了 但是
document.getElementById('<%=Hidden1.ClientID%>').value = "1";
这里赋值无效
Hidden1.Value = "1";
跟踪调试他执行了 但是Hidden1没有值
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "document.getElementById('topcontainer').style.display = 'block';", true);
}
通过Request.Form["__EVENTTARGET"]可以得到引起页面回发的控件
(除了Button,ImageButton这2个控件不能这样使用外)
分给你了
//Hidden1.Value = "1";
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "document.getElementById('topcontainer').style.display = 'block';", true);