开发者

Can you set the css class for the div generated by an update panel from the code behind?

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 dynami开发者_开发百科cally?


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"></script>

<script type="text/javascript">
    $("#<%=udPanel.ClientID%>").addClass("MyClass");
</script>


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.


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


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 + "')");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜