c# - Casting Exception in ASP.NET Web Application -


i have function:

  private void ds_itembound(object sender, datagriditemeventargs e)         {             if (e.item.itemindex >= 0)             {                 e.item.cells[0].controls.add(utility.getimage("delegate"));                  e.item.attributes.add("description", ((system.data.datarowview)(e.item.dataitem)).row["delegatedescription"].tostring());                 string delegatename = ((system.data.datarowview)(e.item.dataitem)).row["delegatename"].tostring();                  e.item.attributes.add("delegatename", ((system.data.datarowview)(e.item.dataitem)).row["delegatename"].tostring());                  ((system.web.ui.webcontrols.checkbox)(e.item.cells[3].controls[0])).checked = false;                   if (hdelegates.value != "")                 {                      string[] selecteddelegates = hdelegates.value.split(new char[] { ',' });                     foreach (string delegate in selecteddelegates)                     {                          if (delegate.equals(delegatename))                         {                             count++;                             ((system.web.ui.webcontrols.checkbox)(e.item.cells[3].controls[0])).checked = true;                              break;                         }                     }                 }             }             if (count == ddelegates.items.count)                 lbleditcheck.checked = true;             else                 lbleditcheck.checked = false;         } 

the ui has table each row corresponding 1 entry. so, each row has image, description, name.

i getting exception in code @ line:

((system.web.ui.webcontrols.checkbox)(e.item.cells[3].controls[0])).checked = false; 

the exception : unable cast object of type 'system.web.ui.literalcontrol' type 'system.web.ui.webcontrols.checkbox'.

further, e.item.cells[3].controls[0] system.web.ui.literalcontrol .

and the check box markup looks <asp:checkbox id="lbleditcheck" runat="server" tabindex="0" width="30px" cssclass="columnheader" /></td>

i stuck on getting work few days now. please let me know if more info useful.

thanks lot help.

what didn't work:

var checkbox = (system.web.ui.webcontrols.checkbox)e.item.findcontrol("lbleditcheck"); checkbox.checked = false; 

in case checkbox null. , corresponding exception thrown.

use ids of controls want , use findcontrol checkbox. use listviewitemtype.dataitem data rows null non dataitem rows.

if (e.item.itemtype == listviewitemtype.dataitem) {     ((system.web.ui.webcontrols.checkbox)(e.item.findcontrol("lbleditcheck")).checked = false; } 

if expect of row may not have checkbox control can first checkbox , change changed status.

checkbox lbleditcheck = e.item.findcontrol("lbleditcheck") checkbox; if(lbleditcheck != null)      lbleditcheck.checked = false; 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -