[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-20
6
Medium Priority
?
409 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:toshi_
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 24183336
I think you'll have to use DataControlFieldCell instead of TableCell
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24183427
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
            }
        }
0
 

Author Comment

by:toshi_
ID: 24183534
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'.

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 6

Expert Comment

by:HarryNS
ID: 24183660
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

0
 
LVL 6

Accepted Solution

by:
HarryNS earned 2000 total points
ID: 24183676
I will explain my sample...

I have the following thing defined in design view...

<asp:TemplateColumn HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
              <ItemTemplate>
                <asp:CheckBox ID="chkSelection" Runat="server" />
                <asp:Label ID="lblCustomerID1" Text='<%# DataBinder.Eval(Container.DataItem,"CustomerID") %>' Runat="server" />
              </ItemTemplate>
            </asp:TemplateColumn>

I use the following code to read the values...

foreach (DataGridItem dgItem in this.dtgTest.Items)
        {
            chkSelected = (CheckBox) dgItem.FindControl("chkSelection");
            if (chkSelected.Checked)
            {
                strCompanyName = ((Label) dgItem.FindControl("lblCompanyName")).Text;
                strCustomerID = ((Label)dgItem.FindControl("lblCustomerID1")).Text;
                lblSelections.Text += "Company Name: <b>" + strCompanyName + "</b> | ";
                lblSelections.Text += "Customer ID: <b>" + strCustomerID + "</b><br>";
            }
        }
0
 

Author Comment

by:toshi_
ID: 24183691
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>
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 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