Solved

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

Posted on 2006-11-02
9
210 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

912 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

17 Experts available now in Live!

Get 1:1 Help Now