# Saturday, November 06, 2004
« It's been a while... | Main | Creating a printable listing of fields i... »

I was faced with a challenge a while back where I needed to trigger a javascript function when an individual item was clicked in a RadioButtonList. Unfortunately, the standard Control.Attributes.Add functionality doesn't work here, because you can't directly access the rendered controls easily with the RadioButtonList. In this case, I wanted an image which indicated a required value to disappear anytime one of the options was clicked.

After a lot of playing around, I came up with this generic function which will append some script to each item in a RadioButtonList. You can further modify this to apply individual scripts to each item in the RadioButtonList.

        /// <summary>
        /// Function to append client script to the onclick event of individual radiobuttonlist buttons
        /// </summary>
        /// <param name="rbl">RadioButtonList to apply script to</param>
        /// <param name="page">The Page the script is going to be appended to</param>
        /// <param name="script">The script to append</param>
        public static void SetRadioButtonListItemScript(RadioButtonList rbl, Page page, string script)
        {
            for (int idx = 0; idx < rbl.Items.Count; idx++)
            {
                RegisterClientObjectFunction(page, rbl, idx, script);
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page">The Page the script is going to be appended to</param>
        /// <param name="rbl">RadioButtonList to apply script to</param>
        /// <param name="idx">the index of the radio button</param>
        /// <param name="script">The script to append</param>
        static private void RegisterClientObjectFunction(Page page, RadioButtonList rbl, int idx, string script)
        {
            StringBuilder sw = new StringBuilder();
            if (!page.IsStartupScriptRegistered(rbl.ClientID + "_" + idx.ToString() + "script"))
            {
                sw.Append(@"<SCRIPT>");
                sw.Append(@"document.all." + rbl.ClientID + "_" + idx.ToString() + ".onclick=function() {" + script + "return true;}");
                sw.Append(@"</SCRIPT>");
                page.RegisterStartupScript(rbl.ClientID + "_" + idx.ToString() + "script",sw.ToString());
            }
        }

The key here is that each item in the list ends up with the ClientID of the RadioButtonList, then an underscore (_), and finally the index of the item. Once you have that, you have the keys to the city, as they say. In the example above, you are applying the same script to all items.

This function came out of a common library of mine. If your just going to place it on the page itself, you don't need the page parameter. You can just use the Page object itself. However, go ahead and throw it into a common library, because once you use it once, it's a pretty good bet you'll need it somewhere else in the future.

Thursday, August 04, 2005 2:53:50 PM (GMT Daylight Time, UTC+01:00)
Please check out some relevant information about casino on net http://casino-on-net.screwy-casino.com/
online casinos http://www.screwy-casino.com/
online casino gambling http://online-casino-gambling.screwy-casino.com/
slot machines http://slot-machines.screwy-casino.com/
roulette http://roulette.screwy-casino.com/
internet casino http://internet-casino.screwy-casino.com/
casino gambling http://casino-gambling.screwy-casino.com/
casino online http://casino-online.screwy-casino.com/
casino games http://casino-games.screwy-casino.com/
online casino http://online-casino.screwy-casino.com/
.
Comments are closed.