Adding objects to javascript array and later accessing them in the same .js file
Long time reader, first time poster!
I'm trying to add Div Objects to an array and trying to access them later when I call my loadViews function. All of my alerts fire, in the proper order, but the array m_Divs is always of length 0. And I'm stumped.
I'm re-registering the script each time on Page_Load, due to it throwing an "Error: Object expected" after each page_load when trying to call the javascript if I don't.
.JS file.
var m_Divs = new Array();
function switchViews(obj) {
alert("switchViews!");
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
if (div.style.display == "none") {
alert("adding div" + div);
window.m_Divs.push(div);
alert("added");
}
else {}
}
function loadViews() {
alert(window.m_Divs.length);
for (i = 0; i < window.m_Divs.length; i++) {
window.m_Divs[i].style.display="";
}
}
开发者_高级运维
switch views is triggered via
<a href="javascript:switchViews('div<%#Eval("ID")%>');">
inside a GridView.
.CS File
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude("TheScript", "Scripts/TheScript.js");
if (!Page.IsPostBack)
{
// Stuff.
}
}
Relevant .aspx file code
<script type="text/javascript" src="Merge.js"></script>
<body onload = "loadViews()">
<form id="form1" runat="server">
Save the the values between postbacks, with for example RegisterHiddenField method on ClientScript.
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerhiddenfield.aspx
There is no in-page persistence of any JavaScript objects, so any time you reload the page (this includes postbacks), the JavaScript execution and any variable values you set will get reset.
If you want your data to persist between any page loads, you'll have to store the data somewhere persistent, like in a database. You'll have to write server-side code to do this.
If you only want data to persist between postbacks, you can:
- Store the data in a hidden field as @Tor suggested
- Change your page so it doesn't do any postbacks (read up on Ajax, using jQuery or similar)
- Use ASP.NET
UpdatePanel
s to do partial page postbacks, which essentially is the same as other Ajax methods except you'll be able to access your form in server-side event handlers (this is inefficient, but maybe in this case you don't care), and the JavaScript environment will stay intact.
http://www.thomasfrank.se/sessionvars.html#comment158
I ended up using this method of session storing, it was overkill for what I needed, but worked out well.
RegisterHiddenField and RegisterClientScriptInclude required that I re-declare them each Page_Load which resulted in the value being reset each time. And Ajax doesn't seem to like integrating with this particular project, it was started back when .net 2.0 was new. Whole other can of worms.
I also had to start storing the "obj" variable in the array and not the "div"variable, as the div object was too complex an to pass around like that.
Thanks for the help guys, I'd upvote if my account was privileged enough =)
精彩评论