Friday, March 16, 2012

Persist Javascript change to CssClass

Hi,

I have a panel in a repeater that has a Javascript Show/Hide that just changes the CSS Class on the Panel. I would like to persist the change to the CSS Class that is made by Javascript after a PostBack but i'm having no luck.

I've tried inheriting from asp:Panel and overriding theSaveViewState() andLoadViewState(), savingg the CssClass in the viewstate here but is doesn't persist and the default always appears on PostBack.

Can anyone help? Some example code is included below...

<asp:Repeater><ItemTemplate><asp:HyperLink> id="HyperLink2" runat="server">... HyperLink has a Javascript on it that changes the CSS Classon the Panel via the ID that is registered in code ...</asp:HyperLink><asp:Panel id="Panel1">"Panel1" Runat="server" CssClass="Hide"> ... Stuff In Here ...</asp:Panel></Itemtemplate></asp:Repeater>

You could put the value in a hidden field and always read from it on the postback.

Thanks for the response...

could you elaborate on what you wrote?

How would I reference the hidden field? If you have the time could you give me an example?


no problem.

The hidden field in html would look like this <input type="hidden" runat="server" id="txtHiddenField"/>

To write to this field you would use this javascript code

var HiddenField = document.getElementById("txtHiddenField");

HiddenField.Value = "NameOfCSSClass";

Then on the postback you can read from it the same way

var HiddenField = document.getElementById(txtHiddenField");

HiddenField.Value will have "NameOfCSSClass";


Thank you very much for the help...

Got it working doing what you suggested...

What got me was the client side JavaScript wasn't changing the CSS class name in the client code on the browser so it wasn't registering as a change. It does change the client code when updating a hidden field via javascript which worked perfectly then on a postback...

LovelyBig Smile

0 comments:

Post a Comment