deb_holmes
asked on
evaluating checkbox in datagrid
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.InvalidCastExceptio n was unhandled by user code
Message="Unable to cast object of type 'System.Web.UI.LiteralCont rol' to type 'System.Web.UI.WebControls .CheckBox' ."
Source="Webkinz"
StackTrace:
at WebKinz.Charms.gridFound_U pdate(Obje ct source, DataGridCommandEventArgs e) in C:\All\Visual Studio 2005\Webkinz\Webkinz\Charm s.aspx.cs: line 126
at System.Web.UI.WebControls. DataGrid.O nUpdateCom mand(DataG ridCommand EventArgs e)
at System.Web.UI.WebControls. DataGrid.O nBubbleEve nt(Object source, EventArgs e)
at System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
at System.Web.UI.WebControls. DataGridIt em.OnBubbl eEvent(Obj ect source, EventArgs e)
at System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
at System.Web.UI.WebControls. Button.OnC ommand(Com mandEventA rgs e)
at System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument)
at System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
When I click update on a row in the grow, I get this error:
System.InvalidCastExceptio
Message="Unable to cast object of type 'System.Web.UI.LiteralCont
Source="Webkinz"
StackTrace:
at WebKinz.Charms.gridFound_U
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Control.Rais
at System.Web.UI.WebControls.
at System.Web.UI.Control.Rais
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
In the ASPX page, inside the datagrid
<asp:TemplateColumn HeaderText="Have?" SortExpression="found">
<ItemTemplate>
<asp:CheckBox runat="server" id="chkFound"
Checked='<%#DataBinder.Eval(Container.DataItem,"found") %>'/>
</ItemTemplate>
</asp:TemplateColumn>
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());
conn.Open();
string queryString = "exec webkinz_charm_forest_all '" + account + "'";
SqlDataAdapter adpt = new SqlDataAdapter(queryString, conn);
DataSet ds = new DataSet();
adpt.Fill(ds);
gridFound.DataSource = ds;
gridFound.DataBind();
gridFound.VirtualItemCount = 40;
conn.Close();
}
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";
}
else
{
Label1.Text = "FALSE";
}
{
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CheckBox chk = (CheckBox)e.Item.Cells[8].
Thanks!