Accessing ASP CheckBoxList and RadioButtonList values with JavaScript

by Andreas Nylin 3. June 2010 13:26

One of the worst controls in the ASP.NET library is the CheckBoxList (or the RadioButtonList). I just can't understand why they have implemented it the way they did. The thing that annoys me the most is that it doesn't render the value of each item in the list in the HTML code. I will describe a workaround for this in this post.

Let's start by creating a simple CheckBoxList:

<asp:CheckBoxList ID="MyCheckBoxList" RepeatLayout="Flow"  runat="server">
   <asp:ListItem Text="One" Value="1" />
   <asp:ListItem Text="Two" Value="2" />
</asp:CheckBoxList>

This is the generated HTML code:

<span id="ctl00_MyCheckBoxList">
   <input id="ctl00_MyCheckBoxList_0" type="checkbox" name="ctl00$MyCheckBoxList$0" />
   <label for="ctl00_MyCheckBoxList_0">One</label><br />
   <input id="ctl00_MyCheckBoxList_1" type="checkbox" name="ctl00$MyCheckBoxList$1" />
   <label for="ctl00_MyCheckBoxList_1">Two</label>
</span>

Note that no value attribute is generated for the input elements even though we specified a value.

If you try to add it as an attribute nothing will happen. This will generate the same HTML code:

foreach (ListItem li in MyCheckBoxList.Items)
{
   li.Attributes.Add("value", li.Value);
}

However, if we change the attribute to something else than value a span will be added to wrap the input checkbox element.

foreach (ListItem li in MyCheckBoxList.Items)
{
   li.Attributes.Add("id", li.Value);
}

This is the generated HMTL code:

<span id="ctl00_MyCheckBoxList">
   <span id="1">
      <input id="ctl00_MyCheckBoxList_0" type="checkbox" name="ctl00$MyCheckBoxList$0" />
      <label for="ctl00_MyCheckBoxList_0">One</label>
   </span><br />
   <span id="2">
      <input id="ctl00_MyCheckBoxList_1" type="checkbox" name="ctl00$MyCheckBoxList$1" />
      <label for="ctl00_MyCheckBoxList_1">Two</label>
   </span>
</span>

This is really odd but now we can at least access the checkbox value on the client side. You could do it like this with jQuery:

$("#<%= MyCheckBoxList.ClientID %>").find(":checkbox").click(function () {
   alert($(this).parent().attr("id"));
});

Or with ASP.NET AJAX:

var checkBoxList = $get("<%= MyCheckBoxList.ClientID %>");
var checkBoxes = checkBoxList.getElementsByTagName("input");
for (var i = 0; i < checkBoxes.length; i++) {
   $addHandler(checkBoxes[i], "click", function () {
      alert(this.parentNode.id);
   });
}

Tags: , ,

Development

blog comments powered by Disqus

Updated Checklist for deploying EPiServer sites

by Andreas Nylin 25. March 2010 10:35

The checklist has been updated with info on EPiServerLog.config and more detailed descriptions. View the updated list in the checklist post or download the PDF version.

Tags:

Development

blog comments powered by Disqus

Checklist for deploying EPiServer sites

by Andreas Nylin 10. January 2010 20:20

There are many tasks that needs to be performed when a new EPiServer web site is deployed and it's easy to forget something. This is a checklist with some of the most common tasks that needs to be done on most new EPiServer web sites. You can download the list as PDF here.

Updated 2010-01-15. Added info on Google Page Speed, IIS Compression, Scheduled jobs and IIS SEO Toolkit.

Updated 2010-01-28. Apparently auto shrinking causes file system fragmentation which slows down performance so it should never be turned on.

Updated 2010-03-25. Added info on EPiServerLog.config. More detailed descriptions.

Licenses


Order license for EPiServer
Make sure that correct license types have been ordered.

Order licenses for EPiServer modules
Make sure that correct license types have been ordered.


Access rights in EPiServer


Check access rights in the EPiServer page tree
Make sure that Administrators and Editors have the correct access levels. Remove everyone from pages that shouldn’t be accessed by normal users.

Remove test users and groups in EPiServer
Go through the list of users and groups and remove those that shouldn’t have access to the site.

Create users and groups in EPiServer (WebAdmins and WebEditors)
Create required users and groups and set their access rights. Create WebAdmins and WebEditors groups for administrators and editors.


Web.config


Set compilation debug to false
Set the debug attribute of the compilation section to false.

Set attributes for mailsettings section
· host = the mail server that should be used to send mails
· from = set to an address or remove
· port, userName, passWord (for mail server if needed)

Search for ":\" and “\\” and check that all paths are correct
Verify that all file system paths are correct.

Check access rights in web.config
Check the authorization section under each location section and verify that the specified groups are correct.


Episerver.config


Set siteUrl = http://www.acme.com

Set uiUrl = http://www.acme.com/UI/

Set utilUrl = http://www.acme.com/Util/

Set SiteDisplayName = Site name used in title, etc

Verify paths for virtualPath/providers
Verify that all paths are correct in the virtualPath section.

Set predefined image formats for the image editor
This is done in the imageEditor > sizePresets section.

Set uiEditorCssPaths (~/EditorStyles.css)
Verify that the path is correct.

Set attributes for outputcache
· httpCacheExpiration = 01:00:00 (one hour)
· httpCacheVaryByParams = * or parameters (id,epslanguage)
· httpCacheVaryByCustom = remove if not needed

Set stringDelayedLoadThreshold = 50
Lazy load is used if a property that inherits from long string is longer than 50 characters.


ConnectionStrings.config


Verify that the correct databases and users are specified


EPiServerLog.config


Set level value="Off"
Logging should be disabled until it’s needed.


ApplicationInitializing.txt


Create an ApplicationInitializing.txt file in the web root.
The text in the file will be displayed for users if the application is busy during initialization. Example content of the file: "The application is loading. Please wait."


Database


Set Recovery mode = Simple

Set Auto shrink = false
Auto shrink causes file-system level fragmentation, which slows down performance.

Remove testusers
Make sure any testusers are removed.


Robots.txt


Create a robots.txt file in the web root
Specify any paths you want to exclude from search engines, such as: /Util/


Google Analytics


Verify that Google Analytics scripts are included in the page and that the correct key is specified.


Validate Pages


Verify that the pages HTML code validates
http://validator.w3.org

Validate the site with IIS SEO Toolkit http://weblogs.asp.net/scottgu/archive/2009/06/03/iis-search-engine-optimization-toolkit.aspx


Browser Compability


Verify that the page looks ok in these browsers:

Internet explorer 9

Internet explorer 8

Internet explorer 7

Internet explorer 6

Firefox

Google chrome

Safari

Opera


Optimization


Analyze the page with Yslow and Google Page Speed
Analyze all JavaScript and CSS files and make sure they are only loaded when needed.

Make sure that any ScriptManagers has ScriptMode set to Release
Script files will be minified when ScriptMode is set to Release.


Favicon


Verify that a favicon is displayed


Title


Verify that the page title is displayed correct


Images

 

Verify that images can be uploaded

Verify that images are displayed

Verify that automatically resized images are displayed (thumbnails, etc)


Mail



Verify that mails sent from forms are sent and received
Test Xforms and any custom built forms.


Search


Perform test searches and verify that relevant results are shown


Error pages


Add custom error pages in IIS

Verify that error pages are displayed for 404, 500, etc


IIS


Create a separate Application Pool for the site
Every site should run in its own Application pool

Disable IIS logging
Logging should be disabled until it’s needed.

Enable Gzip compression in IIS for static and dynamic content
Blog post about HTTP compression in ASP.NET: http://www.stardeveloper.com/articles/display.html?article=2007110401&page=1
This is good resource on how to setup compression in IIS7: http://technet.microsoft.com/en-us/library/cc771003(WS.10).aspx


EPiServer Admin



Verify that scheduled jobs are active
Setup scheduled jobs to run in an appropriate interval.

Tags:

Development

blog comments powered by Disqus

A short summary of jQuery Summit 2009

by Andreas Nylin 21. November 2009 10:17

jQuery summit is an online conference that was held this Thursday. The main event was of course The State of jQuery with John Resig.

John talked a lot about the news in the upcoming version 1.4 of jQuery. Some of the news are:

  • Optimization and improvements of existing functions (some are now three times faster that before)
  • jQuery.core will be split in to several modules.
  • Dynamically loading portions of the library with .getScript().
  • .toArray() method. Returns an array of DOM elements.
  • .get(-N). Access elements with negative indices.
  • .data(). Returns all data for an element.
  • .detach(). Like .remove() but keeps events and data intact.
  • .remove() is now a lot faster.

Other news:


Other interesting presentations were jQuery Anti-Patterns for Performance & Compression with Paul Irish and Refactoring jQuery with Mike Hostetler.

Tags: , , ,

Development

blog comments powered by Disqus

6 great JavaScript resources

by Andreas Nylin 19. November 2009 13:07

http://net.tutsplus.com/tutorials/javascript-ajax/24-javascript-best-practices-for-beginners/
Great tips for the beginner. Examples of common pitfalls and do's and don'ts.

 

https://developer.mozilla.org/En/JavaScript
A really good JavaScript reference and guide. Also has information on what's supported in different versions of JavaScript.

 

http://www.quirksmode.org/js/contents.html
Covers just about everything from how to work with strings to event binding and AJAX. Lots of useful compatibility tables.

 

http://www.crockford.com/javascript/
Douglas Crockford is the Author of JavaScript - the good parts. On his site you will find many great tips on how you should write JavaScript and how to create advanced object inheritance and extending.

 

http://www.asp.net/AJAX/Documentation/Live/ClientReference/default.aspx
API for the ASP.NET AJAX library. There's so much more than just UpdatePanel.

 

http://www.jslint.com/
A JavaScript syntax validator. Run it on all your scripts.

Tags: ,

Development

blog comments powered by Disqus

Expression Web SuperPreview for Windows Internet Explorer

by Andreas Nylin 14. November 2009 18:20

Expression Web SuperPreview for Windows Internet Explorer is the quite long name of a great tool for testing web page layouts in different browsers (I'm just gonna refer to it as SuperPreview form now on).

SuperPreview allows you to compare how a web page is rendered in different browser versions. You can add several browser windows which you can display side by side or on top of each other. You can even add images to compare your web page with a Photoshop design mockup. The full version currently has support for these browsers: Internet explorer 6-8, Firefox and Safari.

Other features are: rulers and guidelines, zoom, browser size, DOM explorer, CSS properties and element highlighting.

SuperPreview is a part of Microsoft Expression Web but there is a free stand-alone version of SuperPreview that only has support for the Internet Explorer browsers.

More info on SuperPreview
Download the stand-alone version
More info on Micorsoft Expression

Tags: , , ,

Design | Development

blog comments powered by Disqus

How to determine if an EPiServer Composer function is placed in another Composer function

by Andreas Nylin 9. November 2009 09:56

Sometimes it can be handy to know if a Composer function is placed directly on the page or if it's placed in another Composer function. Here is a piece of code I wrote that does the trick.
The property must be placed in a class that derives from Dropit.Extension.Core.BaseContentFunction

 

private bool ParentIsContentFunction
{
    get
    {
        // Make sure that the current page is an Composer page
        if (this.CurrentExtensionPageData != null)
        {
            ContentFunctionData currentContentFunctionData = new ContentFunctionData(this.ContentFunctionLink);
            
            // Loop through all content areas in the page
            foreach(ContentAreaData ca in this.CurrentExtensionPageData.ContentAreas)
            {    
                // If any of the content areas contains the current content function it means that the function is placed directly on the page
                // - it's not placed in another content function
                if (ca.ContentFunctions.Contains(currentContentFunctionData))
                    return false;
            }
        }
        
        // If we reach this code it means that the content function is placed in another content function
        return true;
    }
}

Tags: , ,

Development

blog comments powered by Disqus

Fixes for EPiServer Composer client script issues in Internet Explorer and Firefox

by Andreas Nylin 23. October 2009 20:21

In my current EPiServer project (using EPiServer CMS R2 SP2 and EPiServer Composer) I discovered a bunch of client script issues which basically made it impossible to use Composer in any web browser. This post describes these issues, how to fix them and make it possible to use Composer in the latest versions of Internet Explorer and Firefox.

 

Problems in Internet Explorer

  • Script error "Extension.Client not found" when saving content function data in Internet Explorer 7 and 8.
  • Not possible to create functions in Internet Explorer 8 standard mode. Error message when dragging functions to page.

Both of these problems are related to how the setAttribute method of the DOMElement is handled in Internet Explorer. The solution is to pass an empty string as value instead of null.


Fixes:

Add the following code to Dropit\Plugin\Extension\UI\Js\Utils.js

Util.HandleNulls = function(val) {
    return (val == null || val == "null") ? "" : val;
}


Locate the function Extension.ContentFunction.prototype.ToXml in Dropit\Plugin\Extension\UI\Js\Extension.ContentFunction.js

Replace line 444:

contentFunctionNode.setAttribute("remotesite", this.RemoteSite);

with this code:

contentFunctionNode.setAttribute("remotesite", Util.HandleNulls(this.RemoteSite));



Locate the function Extension.ContentArea.prototype.HandleCategoryItem in Dropit\Plugin\Extension\UI\Js\Extension.ContentArea.js

Replace lines 354 and 355:

div.setAttribute("pid", pid);
div.setAttribute("wid", wid);

With this code:

div.setAttribute("pid", Util.HandleNulls(pid));
div.setAttribute("wid", Util.HandleNulls(wid));

 

Problem in Firefox 3.5

Drag and drop doesn't work. Nothing happens when you drag a function from the toolbox.

Fix:

This turned out to be solved. You can read more about it in this thread on EPiServer World. Simply replace your Rico.js file with this one: http://www.m2b.se/ext/composer/rico.js.

Tags: , ,

Development

blog comments powered by Disqus

Developer Lunch: XMPP, BOSH, Strophe and the "Real time web"

by Andreas Nylin 8. October 2009 22:06

Today at the premiere of our developer lunch I decided to share this presentation of Jack Moffitts JavaScript library, Strophe, from the 2009 JS Conf. In this video Jack explains XMPP (eXtensible Messaging and Presence Protocol), BOSH (Bidirectional-streams Over Synchronous HTTP) and gives a quick overview of the Strophe library. I think this is an exciting technique that we will see more and more on the web.

Watch the video at the JSConf website

More info on XMPP
More info on BOSH
More info on Strophe

Sites using the Strophe library:

Speeqe
Chesspark
Drop.io

Tags: , , , ,

Development

blog comments powered by Disqus

Microsoft Ajax content delivery network

by Andreas Nylin 19. September 2009 13:39

Microsoft has launched a content delivery network for client script libraries. It currently hosts the beta version of the new Microsoft Ajax Library and the latest version of jQuery.

More information on Microsoft Ajax CDN

Tags: , , , ,

Development

blog comments powered by Disqus

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


Welcome to the Dropit blog!

Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.