0

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!

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
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!
1

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?

1 Comment

it's being used across different web apps, so they won't neccesarily have any CSS files in common.
0

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>"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.