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
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;
using System.Web.UI.MobileControls.Adapters;
namespace CSSFriendly
{
[SupportsEventValidation]
public class RadioButtonAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
{
private RadioButton _control;
public RadioButtonAdapter()
{
// Do nothing;
}
public new RadioButton Control
{
get
{
return _control;
}
}
protected override void RenderBeginTag(HtmlTextWriter writer)
{
//do.nothing
}
protected override void RenderContents( HtmlTextWriter writer)
{
//add attributes to the Control
writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
writer.AddAttribute(HtmlTextWriterAttribute.Id, Control.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Name, Control.GroupName);
writer.AddAttribute(HtmlTextWriterAttribute.Value, Control.ID);
if (!string.IsNullOrEmpty(Control.CssClass))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, Control.CssClass);
}
if (Control.Checked)
{
writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
}
if (!Control.Enabled)
{
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
}
if (!string.IsNullOrEmpty(Control.ToolTip))
{
writer.AddAttribute(HtmlTextWriterAttribute.Title, Control.ToolTip);
}
if (!string.IsNullOrEmpty(Control.AccessKey))
{
writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, Control.AccessKey);
}
//render the Control
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag(); // </input>
writer.AddAttribute("for", Control.UniqueID);
writer.RenderBeginTag(HtmlTextWriterTag.Label);
writer.Write(Control.Text);
writer.RenderEndTag(); // </label>
Page.ClientScript.RegisterForEventValidation(Control.UniqueID);
}
protected override void RenderEndTag(HtmlTextWriter writer)
{
//do.nothing
}
protected override void OnInit(EventArgs e)
{
_control = (RadioButton)base.Control;
_control.Checked = true;
base.OnInit(e);
}
}
}
None
0 Points
3 Posts
Control Adapter for a RadioButton, (stuck on the
Nov 12, 2007 12:25 PM|jasonhillpdx|LINK
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
None
0 Points
1 Post
Re: Control Adapter for a RadioButton, (stuck on the
Jul 31, 2013 05:52 AM|corbenleek|LINK
Hi,
You have to register the value trough Page.ClientScript.RegisterForEventValidation(list.UniqueID, item.Value) in de render method.
You can also look here
http://chuchuva.com/pavel/2009/12/how-to-use-clientscriptmanager-registerforeventvalidation-method/
control adapter