Building a Control Adapter for the CheckBox control.

Hello all!

I'm currently trying (and failing) to build a Control Adapter to modify how my CheckBox controls are rendered to the page. I don't like the way that by default they render inside a <span> tag. I'd like to change it so that they render inside a label tag.

The good news is: I've managed to make the control render how I'd like; The bad news is: I've completely broken its functionality.

I think the problem is that somehow I've stopped the ASP .NET engine recognising it.

I get the following error:


Server Error in '/Bens' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Does anybody know how I should approach this problem so that the control continues to function as a CheckBox despite my new rendering of it, please?

 For anyone interested in helping me further, I enclose the (abridged) code for the control adapter below: Full code available on request.

 Many thanks



1        public class CheckBoxAdapter: System.Web.UI.WebControls.Adapters.WebControlAdapter
2        {
4            #region Private member variables
6            private CheckBox _control;
8            #endregion
10           public CheckBoxAdapter()
11           {
12               // Do nothing;
13           }
15           public new CheckBox Control
16           {
17               get
18               {
19                   return _control;
20               }
21           }
24           protected override void RenderBeginTag(HtmlTextWriter writer)
25           {
26               if (!String.IsNullOrEmpty(Control.Text))
27               {
28                   if (!String.IsNullOrEmpty(Control.ClientID))
29                   {
30                       writer.AddAttribute(HtmlTextWriterAttribute.For, Control.ClientID);
31                   }
33                   AddCssAttributes(writer);
35                   writer.RenderBeginTag(HtmlTextWriterTag.Label);
36                   writer.Write(Control.Text);
37                   writer.RenderEndTag();
38               }
39           }
42           protected override void RenderContents(HtmlTextWriter writer)
43           {
44               writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
46               AddAccessibilityAttributes(writer);
47               AddFormControlAttributes(writer);
49               if (Control.Checked)
50               {
51                  writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
52               }
54               writer.RenderBeginTag(HtmlTextWriterTag.Input);
55           }
58           protected override void RenderEndTag(HtmlTextWriter writer)
59           {
60               // Do nothing
61           }
64           protected override void OnInit(EventArgs e)
65           {
66               _control = (CheckBox)base.Control;
67               _control.Checked = true;
68               //_control.Enabled = false;
69               base.OnInit(e);
70           }
72   }



10/19/2007 11:46:43 AM 93655 articles. 6 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 51

For those interested, it was quite easy:

protected override void EndRender(HtmlTextWriter writer)





10/19/2007 2:58:44 PM

 Hi Ben,

 I'm staring at the same problem with a radio button. I'd like to try your class with the registerForEvenrValidation fix on it. However, it doesn't compile since a couple of your methods are missing. I can comment them out, but I'm not sure it is working as it should. Any chance you'd share the whole thing?



11/10/2007 1:39:25 AM

Similar Artilces:

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal ...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

Web control in web control
Hi,There are two simple web user controls A and B, where B contains A. They just display the bunch of text boxes, so I do not need them even in tool pane. They sit in the same project next to web form.I noticed that web user control put on ASPX page does not appear in code behind, but if I create it with protected keyword and name being exactly the same as id it works fine. I can access such a web control from code behind and see the value assigned in IE. The problem is that the same routine does work for my A web control in B. When I assign the value IE, I keep getting null reference. ...

controlling an object in a web control from the page the web control is placed in
I have created a web control in that has an image button and an image. when it loads the image button is visible and the image is not. I want to be able from the page which "includes" the web control to set the image button to invisible and the image to visible. When I tried this, the main page didn't recognise the image button or the image (both < asp: objects). help would be gratefully appreciated. oh yeah, it's inline code so any code behind stuff will just confuse me ;) Some code please.-- Justin Lovell...

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

HTML Controls vs Web Form Controls
Hi, I have a page with both controls (HTML and Web Form Controls). I am trying to use an html Calendar control and then get the date selected by the user and put into a Web form input field control. When I try to run this the HTML control cannot talk to the Web form control. Any ideas? Thanks. Can't you put some javascript in the HTML-control with which you assign the value to the ASP.NET Server control? Please post some code so I can help u. Kind Regards,ThaYoung1! Hi here is the Code. Also I added a web form text input control and an html text control and created a fu...

Add .Net Control to Web Custom Control
 I want to add .NET Control (like: TextBox, Button...) to Web Custom Control.How i can do it? Alphavn That's called a composite control -- "Mark As Answer" if my reply helped you -- Hello Guys What gunteman said, that is one approach and in most cases the right one.But its also possible to add child controls to a Control thats inherit from WebControl itself. Be sure to implement INamingContainer to that inherited Control too, thats the important point of that approach.--Microsoft MVP - Visual Developer ASP / ASP.NET-

Web form Controls vs HTML controls
Hi, can anyone explain me the difference between Web Form Controls and HTML controls? What is their usability and disadvantages? Can i write code behind HTML controls?    Thanx in advance. Yes, you just have add "runat=server", in your HTML control. The Web Server Control, have more funtionality that the HTML controls. With Web Server Control, you can work easy with Data. Too, you have controls as AdRotator, WebCalendar and more... : Sergio Tarrillo Here are the Recommendations from Microsoft ....when to use 1 over the other...

web controls and web user control
i have a problem in a web form , I have a webform that contains a web user control which in turn contains a html input form ,the web control events don't fire up ,When I click button ,no effect ,I don't know what is the effect of the html form which is in the web user control on the web controls in the web form ?? can any one help me the web control event should be handle in the code behind page, is the event handler for the button you are clicking registered? You can try double clicking the button, should give you the default btn_click event handler in code behind. Also, make ...

Use Web Control In a Web Control
Is this possible?I am working with an aspx page that consists of many different web controls.One of the web controls contains search criteria.  One of the criteria is a date range where I am trying to add a popup calendar --> which is another web controlCan I register the popupcaledar control on the questionaire control?  I have tried, but nothing seems to be registered.Any ideas will GREATLY help.Relax and have some Home Brew!Prosecute the Owner - Don't PERSECUTE the Breed!Breed Specific Legislation targets the WRONG end of the LEASH! Without seeing your code we can'...

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

using a .Net Control as activex control in VCL form
Hi , We have a to integrate a simple .Dot CSharp control which needs to be integrated into bcb2010 VCL form as a control. We will place the control on the VCL form along with other VCL controls's. I tried creating a simple .net control using a lable+textbox and trying to use in vcl form. I have made the assembly comvisible and generated tlb files and .bpl for the component. I could see the component in "ActiveX" tool palette. But when I drag the object onto VCL form and try to access its attributes I run into problems. I am not able to see .net control on the vcl at runti...

Web Form Server Controls Vs. HTML controls
Hi1. What is the difference between Web Form Server controls and HTML controls with runat="server" attribute.e.g. <input type="text" id="mytext" runat="server"> Vs. <asp:TextBox id="mytext" />2. Why we should not use the normal HTML control instead of Server Controls since HTML controls are rendered on client side the performance in using them will be much better that using Web form server controls. Pls. can somebody explain this.- Anand one thing i know. there are much more options for the web controls. and web controls are easier to use than the http controls. for exampl...

Web resources about - Building a Control Adapter for the CheckBox control. -

Auditorium Building - Wikipedia, the free encyclopedia
... -87.6251722 Coordinates : 41°52′33.95″N 87°37′30.62″W  /  41.8760972°N 87.6251722°W  / 41.8760972; -87.6251722 The Auditorium Building in ...

Home building boom lifts Boral profit 23pc
... Kane said new infrastructure, including major road projects, will drive the Australian economy as resources projects dry up and the home building ...

The ins and outs of planning and building your own home NAS
... ahead and build my own, which would give me the flexibility to build basically whatever box I wanted. Inspired by our recent articles on building ...

Taiwan earthquake: Developer questioned over building collapse
Police in Taiwan are questioning the developer of a building which collapsed in Saturday's earthquake, killing at least 39 people.

Obama wants his presidential library to be like this Seattle building
Denis Hayes had hoped that his Seattle foundation's building, the world-renowned Bullitt Center, would have inspired local real estate developers ...

Building a safer web, for everyone
Today is Safer Internet Day , a moment for technology companies, nonprofit organizations, security firms, and people around the world to focus ...

Amazon Building Global Delivery Business to Take On Alibaba
In recent weeks, speculation has mounted that Inc. plans to launch a global shipping and logistics operation that will compete with ...

8 charts reveal mind-boggling numbers about the monster rocket NASA is building to shuttle astronauts ...
Earlier this month, NASA announced the first mission it will be launching aboard its monster rocket, called the Space Launch System . In 2018, ...

After quake, Taiwan seeks detention of developers of fallen building
More than 40 people killed in the earthquake, with all but two in the actual building collapse; more than 100 still believed to be trapped

Building a website takes tools and skills, here’s how to get both
Leaving your mark on the Internet shouldn’t just extend to the comments section. But to build useful, beautiful things online you need the skillset ...

Resources last updated: 2/11/2016 4:23:47 AM