Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JavaScript dynamically assigned properties don't maintain their state after postback

Posted on 2006-11-02
9
211 Views
Last Modified: 2009-12-16
I have a table with bunch of checkboxes. When i click on the checkbox, i use JavaScript to change the table's background color. When i press the update button, all background properties comes with their default values. Is there any way i can prevent these properties being reset to their default values?

Thanks,
0
Comment
Question by:Carl3003
9 Comments
 
LVL 22

Expert Comment

by:WMIF
ID: 17862821
do you have these javascript's in functions?  you can create one function that calls each function to check all of these properties, and set them appropriately.  then you call this function in the body onload attribute.  if you would like help, please paste the javascript code you are using.
0
 

Author Comment

by:Carl3003
ID: 17862862
the javascript is simple. It change the table's cell backgroung color from red to blue or vice versa. Howevere, what happens after posting back is that the background color returns to its original value.
I have thought of many ways of how to go around this problem but what i am really looking for is a solution that doesnt involve extra coding.
If there is any way to associate my viewstate with table's cell properties so that everytime i postback, the viewstate will maintain the table's background color.
0
 

Author Comment

by:Carl3003
ID: 17862883
function ToggleList(ListId, link)
{
    var childList = document.getElementById(ListId);
    var refImg = link.children[0];
   
    if(childList==null)
        return;

    if(childList!=null && childList.style.display == "none")
    {
        refImg.src = "images/gif/minus.gif";
        childList.style.display = "block";
    }
    else
    {
        refImg.src = "images/gif/plus.gif";
        childList.style.display = "none";
    }        

}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 22

Accepted Solution

by:
WMIF earned 25 total points
ID: 17862968
the only way that i know how to keep those colors would be to create a pageload function.

function pageload() {
  ToggleList(ListId1, link1);
  ToggleList(ListId2, link2);
  ToggleList(ListId3, link3);
  ToggleList(ListId4, link4);
  ToggleList(ListId5, link5);
}


then add it into your body tag:

<body onload="pageload()">
0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 17865270
HI,

i hope you  did change the background color using javascript..

while changing the background color keep your change in a hidden field also!!

ie,

changing Red to Green then
update a hidden field with a value 1

and this value shld check in your body onload

<body onload="initLoad();">

function initLoad()
{
if (document.getelementByid("hiddenId").value == 1)
 {
 // set your color
}
else
{
// set another color
}

And in server code for not postback

set hiddenId.Value = 1 ; // default color

}

Regards
Renju
0
 
LVL 4

Expert Comment

by:satish_nagdev
ID: 17866543
hi carl,
well simply put when you update some values etc of control from javascript they are not persisted on postbacks.
however what you could do is have hidden fields with runat = server, in that set values of properties etc.
like documet.all.hiddenfieldid.value = "somevalue"
then in postback you can get values of properties etc that you want to set like
hiddenfieldid.value = Request["hiddenfieldid"]
control.property.value = hidenfieldid.value

hope it helps

regards,
satish
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question