Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a UserControl which contains, among other things, this AJAX modal popup extender:

<ajax:ModalPopupExtender ID="MPE" runat="server" 
 TargetControlID="btnChangePassword" PopupControlID="pnlPasswordChanging"
 BackgroundCssClass="modalBackground" DropShadow="true"
 CancelControlID="btnCancel" OnCancelScript="ULC_ChangePw_CancelBtnClick();" />

Nothing special here. The problem comes from that BackgroundCssClass attribute—it requires a CSS class called modalBackground. Unfortunately I cannot add a CSS class from a user control in a way that survives postbacks.

If I add my modalBackground class to the .ascx page:

<style type="text/css">
    .modalBackground
    {
        background-color: #A1A1A1;
        filter: alpha(opacity=70);
        opacity: 0.7px;
    }
</style>

...it will show up property when first loaded, but not so after subsequent postbacks. Of course I could define modalBackground within the page itself, or within a seperate, standalone CSS file that is called by the user control, but neither solution will work for me.

Is there no way to create a CSS class programmatically and add it to the page? Basically I'm looking for a CSS equivilant to Javascript's RegisterClientScriptBlock function:

Dim controlNameScript = String.Format("<script type='text/javascript'> var AppMenuName = '{0}' </script>", Me.ClientID)
Me.Page.ClientScript.RegisterClientScriptBlock(myType, "ControlName", controlNameScript)

Thanks!

share|improve this question

3 Answers 3

up vote 1 down vote accepted

If the CSS is defined in your .ASCX file, it should be rendered every time, postback or not. Unless the control itself is set to Visible="false".

One workaround is to define the CSS code within an asp:Literal block on your control. Your control can then expose a public function that simply returns the contents of that literal to the caller. If a host is going to make your control invisible, they can grab the CSS code using that public function, and place it within the head section of the page. In that way, the CSS definition should always be there, regardless of the Visibility setting of the control.

In the larger scheme of things, Adam is correct: it's better to keep your CSS code in .CSS files wherever possible.

share|improve this answer
    
Thanks, this is the kind of answer I was looking for--using a literal control should work for me. (The control isn't invisible, but it is within an update panel, if this makes any difference.) –  J.Steve Jul 7 '11 at 21:09
    
Glad I could help! It definitely seems odd that the CSS wouldn't be rendered - I'm not aware of any UpdatePanel-specific behavior which would cause that to happen; though they have lots of quirky behavior and have fallen out of favor in most ASP.NET circles. If you end up doing any more debugging to find the root cause, I'd definitely be interested in what you find. Best wishes! –  mikemanne Jul 8 '11 at 13:27

It would be standard to include this class in a css file you reference in your site's master page, or the specific page itself. Why would this not work for you?

share|improve this answer
    
it's being used across different web apps, so they won't neccesarily have any CSS files in common. –  J.Steve Jul 7 '11 at 21:07

Okay here's all I needed to solve my problem:

    Me.Page.Header.Controls.Add(
        New LiteralControl("<style type=""text/css""> .modalBackground {background-color: #A1A1A1; filter: alpha(opacity=70); opacity: 0.7px;} </style>"))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.