首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

再开 100 分贴:只差一丁点了,小弟我用javascript和div 实现的无刷新隐藏与显示 div 层的功能已实现,就是不知道当有刷新或回发的时候,页面

2011-12-30 
再开 100 分贴:只差一丁点了,我用javascript和div 实现的无刷新隐藏与显示 div 层的功能已实现,就是不知道

再开 100 分贴:只差一丁点了,我用javascript和div 实现的无刷新隐藏与显示 div 层的功能已实现,就是不知道当有刷新或回发的时候,页面


  pub.aspx   页全部代码如下:

-------------------------------------------------

<script>
function   displayDiv(obj)
{
if(document.getElementById( "divOrders ").style.display== "none ")
{
document.getElementById( "divOrders ").style.display= "block ";
document.getElementById( "btnDisplay ").value= "隐藏 ";
}
else
{
document.getElementById( "divOrders ").style.display= "none ";
document.getElementById( "btnDisplay ").value= "显示 ";
}


}
</script>

<body   >
<form   id= "Form1 "   method= "post "   runat= "server ">
<table>
<tr>
<td> <input   type= "button "   name= "btnDisplay "   value= "隐藏 "   onclick= "displayDiv(document.getElementById( 'divOrders ')) ">
<div   id= "divOrders "   style= "DISPLAY:none ">
<table   width= "95% "   align= "center "   height= "300 ">
<tr>
<td   bgColor= "#66cc33 "> 这里是工作单内容
<asp:Button   id= "Button1 "   runat= "server "   Text= "测试回发后是否能保持住状态 "> </asp:Button>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</form>
</body>

--------------------


如何能实现我说的功能:假如我现在是通过点击

<input   type= "button "   name= "btnDisplay "   value= "隐藏 "   onclick= "displayDiv(document.getElementById( 'divOrders ')) ">

按钮,把 <div   id= "divOrders "   style= "DISPLAY:none ">   层隐藏或是显示了,当这个页面上刷新或是有回发的时候,如何能够保持住当前页面的状态(隐藏或是显示的状态)


再一次请各位高手帮忙!不胜感激!!!




[解决办法]
每次改变后把值放到cookie中,刷新后从cookie取值判断。
只是想法,没试过。
[解决办法]
<script>
function displayDiv(obj)
{
if(document.getElementById( "divOrders ").style.display== "none ")
{
document.getElementById( "divOrders ").style.display= "block ";
document.getElementById( "btnDisplay ").value= "隐藏 ";
}
else
{
document.getElementById( "divOrders ").style.display= "none ";
document.getElementById( "btnDisplay ").value= "显示 ";
}
document.cookie = 'curp= ' + document.getElementById( "divOrders ").style.display;

}

</script>

<body >
<form id= "Form1 " method= "post " runat= "server ">
<table>
<tr>
<td> <input type= "button " name= "btnDisplay " value= "隐藏 " onclick= "displayDiv(document.getElementById( 'divOrders ')) ">
<div id= "divOrders " style= "DISPLAY:none ">
<table width= "95% " align= "center " height= "300 ">
<tr>


<td bgColor= "#66cc33 "> 这里是工作单内容
<asp:Button id= "Button1 " runat= "server " Text= "测试回发后是否能保持住状态 "> </asp:Button>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<script>


function getCookie(cookieName) {
var cookieString = document.cookie;
var start = cookieString.indexOf(cookieName + '= ');
if (start == -1) return null;
start += cookieName.length + 1;
var end = cookieString.indexOf( '; ', start);
if (end == -1) return unescape(cookieString.substring(start));
return unescape(cookieString.substring(start, end));
}
var sta = getCookie( 'curp ');
if(sta)
document.getElementById( "divOrders ").style.display=sta;
</script>
</form>
</body>
[解决办法]
form中加上hidden标签来记录状态 <input type= 'hidden ' id= 'flag ' name= 'flag ' value= ' '>
然后在改变层属性的时候同时把属性赋给hidden
function displayDiv(obj)
{
if(document.getElementById( "divOrders ").style.display== "none ")
{
document.getElementById( "divOrders ").style.display= "block ";
document.getElementById( "btnDisplay ").value= "隐藏 ";
document.getElementById( "flag ").value= "block ";
}
else
{
document.getElementById( "divOrders ").style.display= "none ";
document.getElementById( "btnDisplay ").value= "显示 ";
document.getElementById( "flag ").value= "none ";
}



}
</script>
读取的时候就像上面的写法一样

[解决办法]
使用 一个HtmlInputHidden吧
<Input type=hidden runat=server id= "hidden1 "/>

修改你的js:
displayDiv()
{
...
//if div 显示
document.GetElementByID( "hidden1 ").value=1;
else
document.GetElementByID( "hidden1 ").value= " ";
}


在前台window.Onload事件中加入代码:
function window.Onload()
{
<%if (hidden1.value== "1 "){%>
document.GetElementByID( "divOrders ").style.display= "block ";
<%}else{%>
document.GetElementByID( "divOrders ").style.display= "none ";
<%}%>
}
[解决办法]
放到hidden里面保存,下一次window.onload的时候读取hidden并还原原来的状态。
[解决办法]
<script language= "C# " runat= "server ">
void Page_Load(object src,EventArgs e){
if(IsPostBack){
string dstate=dispState.Value;
if(dstate!= "none "){
string dscript= " <script language=\ "javascript\ "> document.getElementById(\ "divOrders\ ").style.display=\ "block\ ";document.getElementById(\ "btnDisplay\ ").value=\ "隐藏\ "; < "+ "/script> ";
if(!IsStartupScriptRegistered( "dispscript "))
RegisterStartupScript( "dispscript ",dscript);
}
}
}

void testclick(object src,EventArgs e){
Response.Write( "已经回发了,div状态保存住了吗 ");
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= "Webdiyer (www.webdiyer.com) ">
<script>
function displayDiv(obj)
{
if(document.getElementById( "divOrders ").style.display== "none ")
{
document.getElementById( "divOrders ").style.display= "block ";
document.getElementById( "btnDisplay ").value= "隐藏 ";
document.getElementById( "dispState ").value= "block ";
}
else
{
document.getElementById( "divOrders ").style.display= "none ";
document.getElementById( "btnDisplay ").value= "显示 ";
document.getElementById( "dispState ").value= "none ";
}
}
</script>

</HEAD>
<body >
<form id= "Form1 " method= "post " runat= "server ">
<table>
<tr>
<td> <input type= "button " name= "btnDisplay " value= "显示 " onclick= "displayDiv(document.getElementById( 'divOrders ')) ">
<div id= "divOrders " style= "DISPLAY:none ">
<table width= "95% " align= "center " height= "300 ">
<tr>
<td bgColor= "#66cc33 "> 这里是工作单内容
<asp:Button id= "Button1 " runat= "server " Text= "测试回发后是否能保持住状态 " onclick= "testclick "> </asp:Button>
<input type= "hidden " runat= "server " id= "dispState "/>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</form>
</body>

</HTML>

热点排行