views:

269

answers:

4

If I have an UpdatePanel

<asp:UpdatePanel ID="udPanel" runat="server">
            ...
</asp:UpdatePanel>

creates this div when it's rendered

<div id="ctl00_udPanel">

How do you reference this in the code behind to change the css class dynamically?

A: 

From the back of my mind, try udPanel.Attributes.

Arve Systad
unfortunately, Attributes, Class, CssClass, and Style are not options
TheImirOfGroofunkistan
+2  A: 

What you could do is write out the ClientID client-side within javascript and then use jQuery with an AddClass (untested):

<script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

<script type="text/javascript">
    $("#<%=udPanel.ClientID%>").addClass("MyClass");
</script>
CAbbott
probably cleaner to access a normal runat=client element by ID and further to lean on jquery's 'live' method, but in principle clean enough (where a JS requirement is acceptable)
annakata
+1  A: 

Unfortunately, UpdatePanel is considered to be an abstraction that is invisible to the browser (but in reality it isn't). So you can't apply a classname to it.

The only way is to apply the classname to a div or an ASP.NET Panel inside the UpdatePanel.

Chetan Sastry
My problem is the "invisible" update panel is adding space between two controls on my page. I'd like to use the update panel, but it makes the page ugly.
TheImirOfGroofunkistan
A: 

Here's the best I could find to do this dynamically. You can create a javascript function to change the css class which accepts the clientID of the control to change and a string for the css class as parameters. You can set this to be called in the onload() method of the body dynamically passing in the clientID and the css class. This enables you to change the css class for different states of the page.

    HtmlGenericControl body = (HtmlGenericControl)uPanel.Page.Master.FindControl("masterBody");
    body.Attributes.Add("onload", "setCSSClass('" + uPanel.ClientID.ToString() + "', '" + cssClassName + "')");
TheImirOfGroofunkistan