Link to home
Start Free TrialLog in
Avatar of Erwin Pombett
Erwin PombettFlag for Switzerland

asked on

how can i get values from checkbox while looping through a datagrid containing them?

hello experts,

i have a datagrid that contains checkboxes as follow.

                          <asp:TemplateColumn HeaderText="Validated">
                            <ItemTemplate>
                                  <asp:CheckBox Runat="server" ID="chkToValidate" ValueID='<%# DataBinder.Eval(Container.DataItem, "DrainID") %>'  Checked='<%# DataBinder.Eval(Container.DataItem, "validated")%>'></asp:CheckBox>
                            </ItemTemplate>
                     ....
                   </Columns>      
              </asp:DataGrid>

i have created a method to parse the checkboxes when submit.
protected void DeleteValues(object sender , EventsArgs e) {
   foreach(DataGridItem row in DrainGrid.Items){
          CheckBox checkVal = (CheckBox)row.FindControl("chkToValidate");
                if(checkVal.Checked){
                        Response.Write("<br />id checked : " + ((TableCell)checkVal.Parent).Attributes["ValueID"] );
                 }
  }
}


when look navitor source for my datagrid, i can see

<td class="td_75">
  <span valueid="10">
   <input id="DrainGrid_ctl13_chkToValidate" type="checkbox" checked="checked"    name="DrainGrid$ctl13$chkToValidate"/>
  </span>
</td>

so i supposed i should get my value out of checkbox parent, but it's not working.
QUESTION:
how can i revocer the value fro the checkbox value?
where can i write the value if this is not a good idea?

thank you in advance for infos and help.
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

I think you'll have to use DataControlFieldCell instead of TableCell
Avatar of HarryNS
HarryNS

This works fine for me...
CheckBox chkSelected;
        foreach (DataGridItem dgItem in dtg.Items)
        {
            chkSelected = dgItem.FindControl("chkSelection");
            if (chkSelected.Checked)
            {
                //Your Logic
            }
            else
            {
                //Your Logic
            }
        }
Avatar of Erwin Pombett

ASKER

hello HarryNS , hello informaniac,

Harry, i'm looking to recover the value that i put in the span that surrounds the checkbox.


Informaniac,
i receive the following error :
System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.TableCell' to type 'System.Web.UI.WebControls.DataControlFieldCell'.

This will work to get the values from check box. But why you need to have some value assigned to the same column where check box also there...???
CheckBox chkSelected;
        foreach (DataGridItem dgItem in dtg.Items)
        {
            chkSelected = (CheckBox ) dgItem.FindControl("chkSelection");
            if (chkSelected.Checked)
            {
                //Your Logic
            }
            else
            {
                //Your Logic
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HarryNS
HarryNS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i need to recover a value from the columns that was checked,
i bound the value to DrainId, but i cannot recover it.

To top it over, when i set a value to the checkbox "ValueID", i get on webserver, the theckbox surrounded by a span,


if you can tell me how to recover the Id value ?
tell me, but i need the id of the row in order to know what was seelcted.


    <asp:DataGrid
                    id="DrainGrid"
                    border="1"
                    runat="server"
                    pagesize="5"
                    allowsorting="True"
                    autogeneratecolumns="False"
                    showheader="False"
                    borderstyle="solid">
                <AlternatingItemStyle CssClass="alternate"></AlternatingItemStyle>
                 <Columns>
                      <asp:TemplateColumn HeaderText="id">
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "DrainID") %>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_50"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="container_id" >
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "ContainerId")%>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_75"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="ecologid">
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "EcologId")%>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_75"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="Drain date">
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "DrainDate")%>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_150"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="Bags drained">
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "BagsDrained")%>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_50"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="Units drained">
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "UnitsDrained")%>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_100"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="Level At Drain">
                            <ItemTemplate>
                                  <%# DataBinder.Eval(Container.DataItem, "LevelPercent")%>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_75"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="Validated">
                            <ItemTemplate>
                                  <asp:CheckBox Runat="server" ID="chkToValidate" ValueID='<%# DataBinder.Eval(Container.DataItem, "DrainID") %>'  Checked='<%# DataBinder.Eval(Container.DataItem, "validated")%>'></asp:CheckBox>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_75"/>
                      </asp:TemplateColumn>
                      <asp:TemplateColumn HeaderText="ToValidate">
                            <ItemTemplate>
                                  <asp:CheckBox Runat="server" ID="chkToDelete" ValueID='<%# DataBinder.Eval(Container.DataItem, "DrainID") %>'></asp:CheckBox>
                            </ItemTemplate>
                            <ItemStyle cssClass="td_75"/>
                      </asp:TemplateColumn>
                   </Columns>    
            </asp:DataGrid>