evaluating checkbox in datagrid

Posted on 2009-04-18
Medium Priority
Last Modified: 2013-12-17
I have a datagrid that uses a templatecolumn to display a bit value from the database as a checkbox.  The datagrid has update functionality and the one field that I want people to be able to change is to toggle this checkbox.  However I can't figure out how to get at the value of the checkbox.   Relevant code is in three parts although I preusme the problem is in the third part, where the update  statement is found.
When I click update on a row in the grow, I get this error:

System.InvalidCastException was unhandled by user code
  Message="Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.CheckBox'."
       at WebKinz.Charms.gridFound_Update(Object source, DataGridCommandEventArgs e) in C:\All\Visual Studio 2005\Webkinz\Webkinz\Charms.aspx.cs:line 126
       at System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
       at System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

In the ASPX page, inside the datagrid
<asp:TemplateColumn HeaderText="Have?" SortExpression="found">
                      <asp:CheckBox runat="server" id="chkFound" 
                             Checked='<%#DataBinder.Eval(Container.DataItem,"found") %>'/>
Code for loading the datagrid. This works fine to properly populate the checkboxes in every row
protected void loadGrids(string account,string sort)
            SqlConnection conn = new SqlConnection(c.getConnectionString());
            string queryString = "exec webkinz_charm_forest_all '" + account + "'";
            SqlDataAdapter adpt = new SqlDataAdapter(queryString, conn);
            DataSet ds = new DataSet();
            gridFound.DataSource = ds;
            gridFound.VirtualItemCount = 40;
Also in the .cs file, the udpate statment:
        protected void gridFound_Update(object source, DataGridCommandEventArgs e)
            bool b = ((CheckBox)e.Item.Cells[8].Controls[0]).Checked;
            if (b)
                Label1.Text = "TRUE";
                Label1.Text = "FALSE";

Open in new window

Question by:deb_holmes

Assisted Solution

newbieal earned 800 total points
ID: 24177666
The stack trace tells you what is wrong:
 Message="Unable to cast object of type 'System.Web.UI.LiteralControl' to type System.Web.UI.WebControls.CheckBox'."

You have a literal that you're trying to cast to a check box.

Try something like this:
if (((CheckBox)gvr.FindControl("chkid")).Checked == true)  

Taken from this article:

Hope this helps.
LVL 12

Accepted Solution

wht1986 earned 1200 total points
ID: 24177669
Sometimes there is some fun things in a cell that you dont expect.  The grid make liberal use of literals when you dont expect them.  I always try to use the FindControl method.  Your error message indicates to me that control[0] is not a checkbox but a literal.  Try using code like to get the checkbox


Author Closing Comment

ID: 31571898
I had tried using the FindControl method previously and was getting errors on it too.  Apparently hadn't been putting it in the right place and needed the specific syntax.  This works:
CheckBox chk = (CheckBox)e.Item.Cells[8].FindControl("chkFound");  

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question