User886800532 posted
Hi All,
I'm trying to write a control adapter for a Radiobutton, so that I can use the CSSClass property of the server control and not get a <span> wrapped around it. I've included my work so far, but I get an error on postback about Event validation, suggesting
that I use the ClientScriptManager.RegisterForEventValidation method. I've tried to do this, but apparently don't have it in the right place. Could someone take a quick look at my code and point me in the right direction?
Regards,
Jason
1 using System;
2 using System.IO;
3 using System.Web;
4 using System.Web.Configuration;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.MobileControls.Adapters;
9
10 namespace CSSFriendly
11 {
12 [SupportsEventValidation]
13
14 public class RadioButtonAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
15 {
16
17 private RadioButton _control;
18
19 public RadioButtonAdapter()
20 {
21 // Do nothing;
22 }
23
24 public new RadioButton Control
25 {
26 get
27 {
28 return _control;
29 }
30 }
31
32 protected override void RenderBeginTag(HtmlTextWriter writer)
33 {
34 //do.nothing
35 }
36
37 protected override void RenderContents( HtmlTextWriter writer)
38 {
39
40 //add attributes to the Control
41 writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
42 writer.AddAttribute(HtmlTextWriterAttribute.Id, Control.UniqueID);
43 writer.AddAttribute(HtmlTextWriterAttribute.Name, Control.GroupName);
44 writer.AddAttribute(HtmlTextWriterAttribute.Value, Control.ID);
45 if (!string.IsNullOrEmpty(Control.CssClass))
46 {
47 writer.AddAttribute(HtmlTextWriterAttribute.Class, Control.CssClass);
48 }
49 if (Control.Checked)
50 {
51 writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
52 }
53 if (!Control.Enabled)
54 {
55 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
56 }
57 if (!string.IsNullOrEmpty(Control.ToolTip))
58 {
59 writer.AddAttribute(HtmlTextWriterAttribute.Title, Control.ToolTip);
60 }
61 if (!string.IsNullOrEmpty(Control.AccessKey))
62 {
63 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, Control.AccessKey);
64 }
65
66
67 //render the Control
68 writer.RenderBeginTag(HtmlTextWriterTag.Input);
69 writer.RenderEndTag(); // </input>
70
71 writer.AddAttribute("for", Control.UniqueID);
72 writer.RenderBeginTag(HtmlTextWriterTag.Label);
73 writer.Write(Control.Text);
74 writer.RenderEndTag(); // </label>
75
76 Page.ClientScript.RegisterForEventValidation(Control.UniqueID);
77 }
78 protected override void RenderEndTag(HtmlTextWriter writer)
79 {
80 //do.nothing
81 }
82 protected override void OnInit(EventArgs e)
83 {
84 _control = (RadioButton)base.Control;
85 _control.Checked = true;
86 base.OnInit(e);
87 }
88
89 }
90 }