Embedding CSS and JavaScript in User Controls

Thursday, 14 January 2010 13:45 by Tom Gröger


User controls are among the better features that ASP.NET has to offer. I use them either whenever I realize that I can use a piece of code and markup somewhere else, but more frequently I write a User Control when things on a webpage are getting crowded and complicated and I want to split my code into smaller pieces.

Very often these "black box" User controls not only contain Html markup and Server code, but also JavaScript and CSS blocks. It is there when a seemingly simple task can get awfully complex - do you want your JavaScript in the Header or (better) in the bottom of the Page ? do you need <%= %> expressions to embed values like ClientID's into the script ? The same applies to CSS Style blocks, they should be placed in the Html Header in order to accelerate Page rendering. And sometimes you may also need expressions to embed WebResource-Urls or other values into the Styles Sheet.

Well, lots of articles have already been written to address this problems, none of them could truly convince me, until I came up with my own simple solution. Here is a short example:


<asp:Literal ID="DebugInfo" runat="server" Visible="false">
<link href="~/default.css" rel="stylesheet" type="text/css" />
<script src="C:\Eigene Dateien\Visual Studio 2008\scripts\jquery-debug.js"
type="text/javascript"></script>
</asp:Literal> <script runat="server"> protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // load ScriptBlock and replace ClientID's string src = ScriptBlock.Text.Replace( "LABEL_CLIENTID",
this.myLabel.ClientID); // move it to the bottom of our page this.Page.ClientScript.RegisterStartupScript( this.GetType(),
"MyScript", src ); // load CSS and move it into the Header src = Environment.NewLine + CSSBlock.Text; this.Page.Header.Controls.Add( new LiteralControl(src)); } </script>
<!-- Html Markup    -->      
<div id="MyDialog" style="display:none">
   
   <asp:Label ID="myLabel" runat="server"></asp:Label>

</div>


<!-- CSS Block    -->      
<asp:Literal id="CSSBlock" visible="false" runat="server">
    
   <style type="text/css">
      #MyDialog {width:750px; height:365px; }
   </style>
   
</asp:Literal>


<!-- Script Block    -->      
<asp:Literal id="ScriptBlock" visible="false" runat="server">

    <script type="text/javascript">
    
       $(document).ready(function() {
           var objLabel = $("#LABEL_CLIENTID");
           objLabel.html( "My User Control");
       });
    
    </script>
            
</asp:Literal>

 

The first thing that you have to do is to hide the CSS and Script Blocks in Server-Side Literal-Controls with a Visible=False attribute. This way our code is visible to ASP, but is not rendered into the Page:

<asp:Literal ID="DebugInfo" runat="server" Visible="false">
<link href="~/default.css" rel="stylesheet" type="text/css" />
<script src="C:\Eigene Dateien\Visual Studio 2008\scripts\jquery-debug.js"
type="text/javascript"></script>
</asp:Literal>

What happens here is that the ASP Parser finds two References: The first Link-Type points to my CSS File and so resolves all css class-definitions to avoid the dreaded Visual Studio “The class or CssClass value is not defined” warning. Because the Literal Block is invisible to the Page, the Stylesheet itself is not loaded on runtime. You can achieve the same result in one line if you do not need Javascript Intellisense:
 
<link href="~/default.css" rel="stylesheet" type="text/css" 
runat="server" visible="false" />

The second Script Type points to my jQuery VsDoc File. Since this Script is only used as a Reference for the ASP Intellisense Parser we can safely use a local path to the file. Voila – jQuery Intellisense in our User Control. 

Now lets take a look at the Javascript Block:
<asp:Literal id="ScriptBlock" visible="false" runat="server">
   <script type="text/javascript">

     $(document).ready(function() {
     var objLabel = $("#LABEL_CLIENTID");
     objLabel.html( "My User Control");
     });
   </script>
</asp:Literal>



The Javascript code is completely embedded into our ASP Server-Side LiteralControl. No need to mess around with encoding the </script> tag, just write and format your JavaScript Code as if you would do in a static html file. Note the LABEL_CLIENTID token, we will use that later to replace the token with a valid ClientID. Usually this kind of solution to overcome ASP.NET containership naming scheme issues uses code blocks like this :

var ctrl = document.getElementById("<%= myLabel.ClientID %>");


Fortunately we can’t use code blocks inside a Literal Control, so we are forced to use something more elegant than this ugly hack. So how do we get the Code into the Page ? This is preferably done in the Pages OnPreRender Event, which is launched as one of the last events during the creation of a page:

// load ScriptBlock and replace ClientID's 
string src = ScriptBlock.Text.Replace( "LABEL_CLIENTID", this.myLabel.ClientID);


// move it to the bottom of our page
this.Page.ClientScript.RegisterStartupScript( this.GetType(), "MyScript", src );


First we load the Content (= our Script ) of the Literal Control and replace the Token with the targeted Controls ClientID.  If you are using some kind of Script Compressor then this would be the right place to strip comments and white spaces etc. 
Next we use the pages ClientScriptManager to  insert our Script into the bottom of our page. If you prefer to insert it into the Header of your page, you can use the approach that we use to insert the CSS Block
:

// load CSS and move it into the Header
src = Environment.NewLine + CSSBlock.Text;
this.Page.Header.Controls.Add( new LiteralControl(src));

Same procedure here: Load the Css Code from the Literal Control, edit it to your heart’s content and then wrap it into a new LiteralControl to add it to the Page.Header Controls collection.


Using this technique you can easily write self contained User Controls with enclosed Scripts and Styles that embed themselves perfectly into the parent page, and as a bonus your get both CSS and JavaScript Intellisense for external files in your User Control …


A jQuery-Room for RadioGroups

Wednesday, 30 December 2009 16:14 by Tom Gröger

So one day I had enough of reading about the oh-so great jQuery Library and decided to give it a try. After all what could be so exciting about a JavaScript library ? Just a couple of methods to select a Dom element? Man, was I wrong - learning jQuery completely changed my way of programming.

More than a year later now I have moved most of my UI code into my JavaScript Framework, using jQuery as my Control-container and Ajax engine ( and for all the other neat stuff that it does ). One thing I always stumbled upon was the use of Radio buttons; like Check buttons they can be checked or not, but unlike them they can be combined to a Radio group that returns a value. My first shot was to wrap the Radio group into a jQuery object and then work on it:

<div id="container">
<input type="radio" id="rad1" name="radKult" value="1" />
<label for="rad1">free drinks</label>
<input type="radio" id="rad2" name="radKult" value="2" />
<label for="rad1">paid drinks</label>
</div>
// Create RadioGroup wrapper and set/get value
jGroup = $("#container input:radio");
var sel = jGroup.filter(':checked').val()
jGroup.attr("checked", "checked");


Ok, that worked somehow, but honestly it was a pain in the rear and most of the time I found myself working with the Dom elements again. The other problem I had with this approach was that while I could easily enable or disable the Radio buttons, but not the attached Labels.

So, finally I came across this problem once too many and sat down to write a jQuery plug-in:


The RadioGroup Control:


The Control should be an extended jQuery object with some methods added and others overwritten. The jQuery part would only wrap the radio buttons,  and the inner class keeps its own a list of radio button objects and associated labels.

  • The val() method is overwritten to easily return and set the Radio group's value
  • The enable() and disable() methods should enable and disable either the complete group (including labels) or only selected buttons
  • The modified get() method will return an extended Radio button object that also understands checked/enable/disable messages


The Code itself is simple and straightforward, inside my controls I do prefer to use the DOM methods ( for performance reasons ) whenever possible:


   ////////////////////// RadioGroup //////////////////////////
    

$.fn.RadioGroup = function() {
/// <summary>
/// JQuery plugin that creates a TomSoft.Form.RadioGroup object and
/// attaches this to the Wrapper of the RadioGroup-Container.
/// <returns type="jQuery" />

if (this.length != 1) {
throw new Error("bad selector for RadioGroup " + this.selector,
"window.document");
return this;
}

var items = [];
_init(this);

//
// JQuery object extensions
//
this.val = function(value) {
/// <summary>
/// Returns the value of the checked radio button within the Group
/// or checks the radiobutton with the given value. if the value
/// does not exist then the radiogroup remains unchecked
/// </summary>

if (arguments.length) {
value = value.toString(); // setter
for (var i = 0; i < items.length; ++i) {
var j = items[i];
j.checked(false);
if (value == j.val()) {
j.checked(true);
break;
};
};
return this;
}
// getter
var j = this.selected();
return ((j) ? j.val() : -1);
};

this.enable = function(bEnable) {
/// <summary>
/// enable/disable RadioGroup and associated Labels
/// </summary>
bEnable = (typeof (bEnable) == 'boolean') ? bEnable : true;
for (var i = 0; i < items.length; ++i) {
items[i].enable(bEnable);
}
return this;
};

this.disable = function() {
/// <summary>
/// if called without arguments the method disables all RadioButton
/// and associated Labels. If numeric arguments are passed then
/// all RadioButtons of the group are enabled except those with
/// matching the index position.
/// </summary>
/// <example>
/// $Controls.radioGroupHerkunft.disable(1,4,5);
/// </example>
if (arguments.length) {
for (var i = 0; i < items.length; ++i) {
// check if pos i is in the argument list to be disabled
var bEnable = ($.inArray(i, arguments) == -1);
items[i].enable(bEnable);
};
return this;
};
return this.enable(false);
};

this.selected = function() {
/// <summary>
/// Returns the checked radiobutton within the Group
/// </summary>
for (var i = 0; i < items.length; ++i) {
if (items[i].checked()) {
return items[i];
};
};
return null;
};

this.get = function(index) {
/// <summary>
/// Returns modified RadioButton Object by position or ID(s):
///
/// radio.get( 0 ) returns first jRadioLabel
/// radio.get( 0,2,3 ) returns Array of buttons in Pos 0,2 and 3
/// usage: $.each( radio.get(0,..),
/// function(){this.disable()})
/// radio.get("checked") return selected jRadioLabel
/// radio.get("all") return list array of all Radio Buttons
/// </summary>

var list = [];

for (var cx = 0; cx < arguments.length; ++cx) {
var arg = arguments[cx];
if (typeof (arg) == 'string') {
if (arg == "checked") return this.selected();
if (arg == "all") return items;

for (var i = 0; i < items.length; ++i) {
if (items[i].radio.id == arg)
list.push(items[i]);
};
}
else if (arg >= 0 && arg < items.length) {
list.push(items[arg]);
};
};
if (list.length)
return (list.length == 1) ? list[0] : list;

return;
};

return this;


//
// Initialize the object
//
function _init(jObj) {

var jLabel = $("label", jObj);
var jRadio = $(":radio", jObj);
for (var i = 0; i < jRadio.size(); ++i) {
items.push(_jRadioLabel(jRadio.get(i), jLabel.get(i)));
};
};

//
// Creates a modified jQuery Object-Wrapper for the given Radio/Label pair
// The jQuery radio Object has some added methods and properties:
// .radio: returns the dom-RadioButton Object
// .label returns the jQuery-Label Object
// .checked: gets/sets the checked state of the RadioButton
//
function _jRadioLabel(radio, label) {

var jRadio = $(radio);

jRadio.radio = radio;
jRadio.label = $(label);

if (jRadio.label.size() == 1)
label.htmlFor = radio.id;

jRadio.checked = function(bChecked) {
/// <summary>
/// gets or sets the checkded state of the RadioButton
/// </summary>
/// <returns type="boolean">checked state</returns>
if (arguments.length) {
bChecked = (typeof (bChecked) == 'boolean') ? bChecked : true;
radio.checked = bChecked; // setter
return jRadio;
};
return radio.checked; // getter
};

jRadio.enable = function(bEnable) {
/// <summary>
/// enable/disable RadioButton and associated Label
/// </summary>
bEnable = (typeof (bEnable) == 'boolean') ? bEnable : true;
radio.disabled = !bEnable;
jRadio.label.css("color", bEnable ? "" : "#ccc")
if (!bEnable) radio.checked = false;
return jRadio;
};

jRadio.disable = function() {
/// <summary>
/// diables RadioButton and associated Label
/// </summary>
return jRadio.enable(false);
};

return jRadio;
};
};


Using the RadioGroup Control:



The usage should be intuitive and easy, yet give you access to every single element of the Radio Group:
jGroup=$("#container").RadioGroup();  // Create extended RadioGroup Object
jGroup.disable(0);                    // disable the first button
jGroup.disable(0,3,4);                // disable a list of radio buttons
jGroup.val("1"); // Set a value and so select a button db.IDRadio = jGroup.val().toInt(); // read the radio group into a field ..
jGroup.get(0); // returns first button as jQuery object jGroup.get(1,2); // returns Array of two jQuery radio buttons jGroup.get("checked"); // return selected jQuery radio button
var domRadio = jGroup.get(0).radio; // access the radio Dom object var domLabel = jGroup.get(0).label; // access the label Dom object

 

Resources:




  ,
  ASP.NET | JavaScript | jQuery
  E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed