CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!
Free 3 Months



Zone: > NEWSGROUP > Asp.Net Forum > microsoft_downloads.css_friendly_control_adapters Tags:
Item Type: NewsGroup Date Entered: 9/22/2006 11:38:23 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 14 Views: 62 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
15 Items, 1 Pages 1 |< << Go >> >|
Russ Helfand
Asp.Net User
RadioButtonList adapter (for Yuval)9/22/2006 11:38:23 PM

0/0

In an earlier posting in this forum, an author asked how to fix the Login adapter so its "remember me" checkbox worked properly.  See http://forums.asp.net/thread/1400630.aspx. Towards the end of that thread, Yuval (ykarj) asked a somewhat different question about how to build a RadioButtonList adapter that handled event validation properly. Rather than continuing to answer Yuval's question in the "remember me" posting, I'm starting this new thread.

Here is the answer for Yuval...

Start with this test page:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:RadioButtonList id=RadioButtonList1 runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>
         <asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
    </div>
    </form>
</body>
</html>

Then you can add this adapter:

using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CSSFriendly
{
    public class RadioButtonListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
    {
        public RadioButtonListAdapter()
        {
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
        }

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            RadioButtonList ButtonList = Control as RadioButtonList;
            if (null != ButtonList)
            {
                int i = 0;
                foreach (ListItem li in ButtonList.Items)
                {
                    string itemID = string.Format("{0}_{1}", ButtonList.ClientID, i++);
                    writer.AddAttribute("for", itemID);
                    writer.RenderBeginTag(HtmlTextWriterTag.Label);
                    writer.AddAttribute("id", itemID); writer.AddAttribute("type", "radio");
                    writer.AddAttribute("name", ButtonList.UniqueID);
                    writer.AddAttribute("value", li.Value);
                    if (li.Selected)
                    {
                        writer.AddAttribute("checked", "checked");
                    }
                    writer.RenderBeginTag(HtmlTextWriterTag.Input);
                    writer.RenderEndTag();
                    writer.Write(li.Text);
                    writer.RenderEndTag();
                    writer.WriteLine();
                    Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID, li.Value);
                }
                Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID);
            }
        }
    }
}

Notice that I've got two distinct calls to RegisterForEventValidation.  Yuval's code was missing the first of these two calls but I only figured it out by looking at disassembled code so it certainly isn't something that is obvious to anyone!

Using this adapter (with the proper additions to refer to the adapter through the .browser file in App_Browsers) you can submit the test page and it won't throw exceptions due to validation problems.


Russ Helfand
Groovybits.com
ykarj
Asp.Net User
Re: RadioButtonList adapter (for Yuval)9/23/2006 12:02:44 AM

0/0

Thanks! That solves the problem.

Falcon
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/13/2007 7:48:18 PM

0/0

Sorry to resurrect an old thread, but this seems pretty relevant to the discussion above.

 

1    using System;
2    using System.Data;
3    using System.Collections;
4    using System.Configuration;
5    using System.Web;
6    using System.Web.Security;
7    using System.Web.UI;
8    using System.Web.UI.WebControls;
9    using System.Web.UI.WebControls.WebParts;
10   using System.Web.UI.HtmlControls;
11   
12   namespace WebServices.CssFriendlyAdapters
13   {
14       public class RadioButtonListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
15       {
16           public RadioButtonListAdapter()
17           {
18           }
19   
20           protected override void RenderBeginTag(HtmlTextWriter writer)
21           {
22               writer.WriteBeginTag("ul");
23               if (Control.CssClass != null && Control.CssClass.Length > 0)
24               {
25                   writer.WriteAttribute("class", Control.CssClass);
26               }
27               writer.Write(HtmlTextWriter.TagRightChar);
28           }
29   
30           protected override void RenderEndTag(HtmlTextWriter writer)
31           {
32               writer.WriteEndTag("ul");
33           }
34   
35           protected override void RenderContents(HtmlTextWriter writer)
36           {
37               RadioButtonList ButtonList = Control as RadioButtonList;
38               if (null != ButtonList)
39               {
40                   int i = 0;
41                   foreach (ListItem li in ButtonList.Items)
42                   {
43                       string itemID = string.Format("{0}_{1}", ButtonList.ClientID, i++);
44                       writer.WriteBeginTag("li");
45                       writer.Write(HtmlTextWriter.TagRightChar);
46                       writer.WriteBeginTag("input");
47                       writer.WriteAttribute("id", itemID);
48                       writer.WriteAttribute("type", "radio");
49                       writer.WriteAttribute("name", ButtonList.UniqueID);
50                       writer.WriteAttribute("value", li.Value);
51                       if (li.Selected)
52                       {
53                           writer.WriteAttribute("checked", "checked");
54                       }
55                       writer.Write(HtmlTextWriter.TagRightChar);
56                       writer.WriteEndTag("input");
57                       writer.WriteBeginTag("label");
58                       writer.WriteAttribute("for", itemID);
59                       writer.Write(HtmlTextWriter.TagRightChar);
60                       writer.Write(li.Text);
61                       writer.WriteEndTag("label");
62                       writer.WriteEndTag("li");
63                       writer.WriteLine();
64                       Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID, li.Value);
65                   }
66                   Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID);
67               }
68           }
69       }
70   }

 

That's my RadioButtonList adapter. It's a bit different from the one above, but pretty similar. My problem is that a RequiredFieldValidator won't catch when nothing is selected in the adapted RadioButtonList. The RequiredFieldValidator works on an unadapted RadioButtonList just fine. What am I missing? Could someone point me in the right direction?

Much appreciated! =)
 

Falcon
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/13/2007 8:02:41 PM

0/0

An update:

It turns out that my code works with server-side validation. It's just that client-side validation won't find anything wrong so the error doesn't show up until all of the client-side errors are gone. I guess I can live with that, even though it would be nice if the client-side validation would work too.
bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/14/2007 2:25:34 AM

0/0

Go to the CodePlex site for the CSSFriendly adapters and you'll see someone proposed something similar: http://www.codeplex.com/cssfriendly/Thread/View.aspx?ThreadId=8165

Earlier this week I adapted the code in that post and tested it with both server and client-side validation and postback events. I'm hoping to test it more tonight and check it in.

Among the implementation I have for it:

  • The RadioButtonList is rendered as an unordered list <ul>, with each radio button rendered in a list item <li> tag.
  • A <div> wraps the markup, and is assigned the CSS class AspNet-RadioButtonList.
  • Each list item is assigned the CSS class AspNet-RadioButtonList-Item.
  • The CssClass defined for the <asp:RadioButtonList> control is applied to the unordered list (<ul> tag).

Of course the TFS is down in CodePlex, so I couldn't check it in tonight if I wanted to, but the implementation (very similar to yours) works. The only significant difference I can see is I wrap the entire content in a DIV tag.

Here's the code of the RadioButtonListAdapter I am working on.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CSSFriendly
{
	/// <summary>
	/// Overrides the default table layout of the <see cref="System.Web.UI.WebControls.RadioButtonList"/>
	/// control, to a XHTML unordered list layout (structural markup).
	/// </summary>
	public class RadioButtonListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
	{
		private const string WRAPPER_CSS_CLASS = "AspNet-RadioButtonList";
		private const string ITEM_CSS_CLASS = "AspNet-RadioButtonList-Item";

		protected override void RenderBeginTag(HtmlTextWriter writer)
		{
			// Div
			writer.AddAttribute(HtmlTextWriterAttribute.Class, WRAPPER_CSS_CLASS);
			writer.AddAttribute(HtmlTextWriterAttribute.Id, this.Control.ClientID);
			writer.RenderBeginTag(HtmlTextWriterTag.Div);
			writer.Indent++;

			// Ul
			if (!string.IsNullOrEmpty(this.Control.CssClass))
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, this.Control.CssClass);
			}
			writer.RenderBeginTag(HtmlTextWriterTag.Ul);
		}

		protected override void RenderEndTag(HtmlTextWriter writer)
		{
			writer.RenderEndTag(); // Ul
			writer.Indent--;
			writer.RenderEndTag(); // Div
		}

		protected override void RenderContents(HtmlTextWriter writer)
		{
			RadioButtonList buttonList = this.Control as RadioButtonList;
			if (buttonList != null)
			{
				int item = 0;

				foreach (ListItem li in buttonList.Items)
				{
					string itemID = string.Format("{0}_{1}", buttonList.ClientID, item);

					// Li
					writer.AddAttribute(HtmlTextWriterAttribute.Class, ITEM_CSS_CLASS);
					writer.RenderBeginTag(HtmlTextWriterTag.Li);

					if (buttonList.TextAlign == TextAlign.Right)
					{
						RenderRadioButtonListInput(writer, buttonList, li, itemID);
						RenderRadioButtonListLabel(writer, li, itemID);
					}
					else // TextAlign.Left
					{
						RenderRadioButtonListLabel(writer, li, itemID);
						RenderRadioButtonListInput(writer, buttonList, li, itemID);
					}

					writer.RenderEndTag(); // </li>
					Page.ClientScript.RegisterForEventValidation(buttonList.UniqueID, li.Value);
					item++;
				}

				Page.ClientScript.RegisterForEventValidation(buttonList.UniqueID);
			}
		}

		private void RenderRadioButtonListInput(HtmlTextWriter writer, RadioButtonList buttonList, ListItem item, string itemID)
		{
			// Input
			writer.AddAttribute(HtmlTextWriterAttribute.Id, itemID);
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
			writer.AddAttribute(HtmlTextWriterAttribute.Name, buttonList.UniqueID);
			writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value);
			if (item.Selected)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
			}
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			writer.RenderEndTag(); // </input>
		}

		private void RenderRadioButtonListLabel(HtmlTextWriter writer, ListItem item, string itemID)
		{
			// Label
			writer.AddAttribute("for", itemID);
			writer.RenderBeginTag(HtmlTextWriterTag.Label);
			writer.Write(item.Text);
			writer.RenderEndTag(); // </label>
		}
	}
}
  
- brian
- blog: www.sidesofmarch.com
wegier-r
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/17/2007 9:17:14 AM

0/0

Hello everybody,
 
I'm new in this forum.
 
First of all I'd like to thanko you for sharing the source code with us. It was very helpful to me. When using the control adapter I noticed, that no postbacks are done, even if I set the AutoPostback of the RadioButtonList to "true" and assign an OnSelectedIndexChanhe-event. As I'm a greenhorn with control adapters I wanted to kindly ask you, if someone has a hint for me, where the problem might be.
 
Best wishes from Switzerland,
 
Richie
bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/18/2007 4:52:30 AM

0/0

I've confirmed that AutoPostBack does not work. Not sure why. I'll do some research.

 


- brian
- blog: www.sidesofmarch.com
bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/18/2007 5:06:58 AM

0/0

Check out changeset 957 (http://www.codeplex.com/cssfriendly/SourceControl/DownloadSourceCode.aspx?changeSetId=957)

I hacked the AutoPostBack support. Let me know if it works in your situation.

Note that AutoPostBack seems to disregard client-side validation (though server-side validation works). Not sure if this is normal behavior (it's late and I'm tired). Let me know if it isn't. ;)


 


- brian
- blog: www.sidesofmarch.com
bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/18/2007 3:31:12 PM

0/0

Changeset 986 has some more mods to the RadioButtonListAdapter:

  • Disabled list items are now disabled in markup.
  • AutoPostBack only applied when controls are enabled.
  • Added walkthru tests for autopostback and onselectedindexchanged events.
Feedback appreciated. :)

- brian
- blog: www.sidesofmarch.com
Suimple
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/30/2007 8:04:06 PM

0/0

I have made some mods to this adapter for checkboxlist and am running into an issue. The list renders as it should however when I try and post the data I get ERROR:

startIndex cannot be larger than length of string.
Parameter name: startIndex

Has anyone run into this issue before using this adapter?

bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/30/2007 8:24:16 PM

0/0

Can you post your code? Looking at it will help us figure out the problem (as well as potentially incorporate it into the adapters).

 


- brian
- blog: www.sidesofmarch.com
Suimple
Asp.Net User
Re: RadioButtonList adapter (for Yuval)4/30/2007 8:50:50 PM

0/0

The code for the adapter below, I am creating the asp:checkboxlist controls in code and am wondering if maybe there is some sort of viewstate issue with the adapter that I need to address.

 

using System;
using System.IO;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Common.Lib.Web.UI.Adaptors
{
    public class CheckBoxListAdaptor : System.Web.UI.WebControls.Adapters.WebControlAdapter
    {
        private const string ITEM_CSS_CLASS = "checklist-item";


        private WebControlAdapterExtender _extender = null;
        private WebControlAdapterExtender Extender
        {
            get
            {
                if (((_extender == null) && (Control != null)) ||
                    ((_extender != null) && (Control != _extender.AdaptedControl)))
                {
                    _extender = new WebControlAdapterExtender(Control);
                }

                System.Diagnostics.Debug.Assert(_extender != null, "CSS Friendly adapters internal error", "Null extender instance");
                return _extender;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            if (Extender.AdapterEnabled)
            {
                Extender.RenderBeginTag(writer, "checkbox-list-" + Control.ID.ToString());
                // ul
                if (!string.IsNullOrEmpty(this.Control.CssClass))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Format("{0}-list-{1}", this.Control.CssClass, this.Control.ID));
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            }
            else
            {
                base.RenderBeginTag(writer);
            }
        }

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
            if (Extender.AdapterEnabled)
            {
                writer.RenderEndTag(); // Ul
                writer.Indent--;
                Extender.RenderEndTag(writer);
            }
            else
            {
                base.RenderEndTag(writer);
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (Extender.AdapterEnabled)
            {
                CheckBoxList checkList = this.Control as CheckBoxList;
                if (checkList != null)
                {
                    int item = 0;

                    foreach (ListItem li in checkList.Items)
                    {
                        string itemID = string.Format("{0}_{1}", checkList.ClientID, item);

                        // Li
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, ITEM_CSS_CLASS);
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);

                        if (checkList.TextAlign == TextAlign.Right)
                        {
                            RenderCheckBoxListInput(writer, checkList, li, itemID, item);
                            RenderCheckBoxListLabel(writer, li, itemID);
                        }
                        else // TextAlign.Left
                        {
                            RenderCheckBoxListLabel(writer, li, itemID);
                            RenderCheckBoxListInput(writer, checkList, li, itemID, item);
                        }

                        writer.RenderEndTag(); // </li>
                        Page.ClientScript.RegisterForEventValidation(checkList.UniqueID, li.Value);
                        item++;
                    }

                    Page.ClientScript.RegisterForEventValidation(checkList.UniqueID);
                }

            }
            else
            {
                base.RenderContents(writer);
            }
        }

        private void RenderCheckBoxListInput(HtmlTextWriter writer, CheckBoxList checkList, ListItem li, string itemID, int item)
        {
            // Input
            writer.AddAttribute(HtmlTextWriterAttribute.Id, itemID);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, checkList.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, li.Value);
            if (li.Selected)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
            }
            if (checkList.AutoPostBack)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
                    String.Format(@"setTimeout('__doPostBack(\'{0}${1}\',\'\')', 0)",
                        checkList.ClientID, item.ToString()));
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag(); // </input>
        }

        private void RenderCheckBoxListLabel(HtmlTextWriter writer, ListItem item, string itemID)
        {
            // Label
            writer.AddAttribute("for", itemID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write(item.Text);
            writer.RenderEndTag(); // </label>
        }
    }
}

bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)5/1/2007 1:58:35 PM

0/0

I'm adding a CheckBoxList adapter shortly, heavily based on the RadioButtonListAdapter. The test page works fine with postbacks and events.

 


- brian
- blog: www.sidesofmarch.com
bdemarzo
Asp.Net User
Re: RadioButtonList adapter (for Yuval)5/1/2007 2:35:59 PM

0/0

And FYI, the problem was rooted here:

 writer.AddAttribute(HtmlTextWriterAttribute.Name, checkList.UniqueID);

The name needs to be unique for the item not the check list. See the updated code (I'm checking it in after I post this) for more details.

 


- brian
- blog: www.sidesofmarch.com
vndotnet79
Asp.Net User
Re: RadioButtonList adapter (for Yuval)7/23/2007 11:12:07 AM

0/0

Hi,

I'm new to this ControlAdapter.

I have been assigned a task to create readonly controls(textbox, checkbox, radiobutton, linkbutton) based on a key setting in database.

I have managed to do that but the events are not firing up for checkbox.

Can you guys help me?

Many thanks in advance,

vndotnet79

15 Items, 1 Pages 1 |< << Go >> >|


Free Download:


Web:
RadioButtonList adapter (for Yuval) - ASP.NET Forums Towards the end of that thread, Yuval (ykarj) asked a somewhat different question about how to build a RadioButtonList adapter that handled ...
RememberMe control in Login adapter "forgets" - ASP.NET Forums I've posted an answer for Yuval regarding how to build a RadioButtonList adapter that passes event validation at this new thread, ...
Delphi 2.0x freeware [Mega Library - All files] ... checkbox and radiobutton have, but Borland forgot to include in the VCL ...... inactive or all) services (Win32, adapter, driver, interactive or all). ...
jGuru: Swing FAQ Author: Yuval Zukerman (http://www.jguru.com/guru/viewbio.jsp? ...... yes but this second adapter doesn't function when the list selection event works at ...
microsoft.public.dotnet.framework.adonet: By Thread Design considerations for BLL objects Yuval (02/27/04); Custom Data Provider Jerry (02/26/04 ..... Data Adapter problems on nullable columns JezB (02/23/04) ...
Bloglines | Blogs Thanks to Davee, Evelyn Mitchell, Michael and Yuval. ...... In this article, filtering queries will be automatically added to each table adapter for each ...
re: Why "I" Write Book$ - Dino Esposito's WebLog NET adapters... " No not that kind of properties, more like access to query ...... for a while (affects DropDownLists and RadioButtonLists, at least). ...




Search This Site:










download file of any extension.

launching aspx page on domain??

calling outlook web access/exchange from asp.net

html in vs and .net

system.datetime to get date for particular day of week

refresh page

moving asp.net website to asp.net web application

asp.net poll

look for the best way to write sequence codes

value of variable into html with no server control...

itemplate

match from begging of line...

session variables

netfile

generate asp.net maker

parser error

new window from internet explorer

sql result in integer

specified method is not supported

getting the ip address

cookie persistence in a new browser window

lots of help

sitemap control

cannot logout because of postback and validation control conflict

appending controls to a panel

download control

emitting js from a composite control

how to replace a part of web.config with an xml file

database search results

request.isconnectionsecure not recognizing ssl.

  Privacy | Contact Us
All Times Are GMT