Specified Cast is Invalid - Telerik RadGrid asp:CheckBox

Great solution to the "specified cast is invalid" problem while binding an asp.net CheckBox control inside of an Telerik RadGrid FormTemplate.

ASP.NET Classic Controls - Grid Forum - asp:Checkbox in FormTemplate. "Specified Cast is Invalid" .

From Fabian Schulz:
I ran across the same problem and have created a simple solution, which is most easy to use:

Create a usercontrol with the following code:

myCheckbox.ascx:

myCheckbox.ascx.cs:
public partial class Controls_myCheckBox : System.Web.UI.UserControl
{


private bool m_checked = false;

public object Checked
{
get { return m_checked; }
set
{
if (value.GetType() == DBNull.Value.GetType())
m_checked = false;
else if (value == null)
m_checked = false;
else if (value.GetType() == typeof(bool))
m_checked = (bool)value;
else
m_checked = false;
}
}


protected void Page_Load(object sender, EventArgs e)
{
m_checked = CheckBox1.Checked;
}




protected void Page_PreRender()
{
CheckBox1.Checked = m_checked;
}
}


In your aspx page you need to register that control in the head:

And now - you can use it like a regular checkbox without the Null problem:

Hope this helps!

Fabian

Comments

Popular posts from this blog

Android ListView with CheckBox - The easy way

Starting an Activity from ItemizedOverlay

WhoCalled