Solved

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

Posted on 2006-11-02
9
208 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now