简体   繁体   中英

Get ID from each EditItemTemplate from a Gridview

I have a gridview with many columns and rows. I need to get the Checkbox Status for each one of the Template fields from the gridview.

I know I can achieve this by getting each individual:

CheckBox cbTrans = (CheckBox)gvStations.Rows[rowindex].FindControl("cbTaxEdit");
CheckBox cbReg = (CheckBox)gvStations.Rows[rowindex].FindControl("cbRegEdit");

If I have 20 template fields how can I get all of them one at a time. My idea is to get one a time in a for loop and perform an operation for each checkbox.

//Something along this lines but I know I can't use J here
for (int i = 0; i <= this.gvStations.Rows.Count - 1; i++)
{
  for (int j = 0; j <= this.gvSta.Columns.Count - 1; j++)                
  {
    //I WANT TO GO THRU EACH COLUMN, J can't be used here, throws error
    CheckBox cbox = (CheckBox)gvStations.Rows[i].FindControl(j);

    if(cbox.Checked)
    {
      //Perform Operation
    } 
 } 
}

Gridview

  <asp:TemplateField HeaderText="TAX" SortExpression="Taxes">
       <EditItemTemplate>
           <asp:CheckBox ID="cbTaxEdit" runat="server" Checked='<%# (int)Eval("Taxes") == 1 %>' />
       </EditItemTemplate>
       <ItemTemplate>
                 <asp:CheckBox ID="cbTax" runat="server" Enabled="false" Checked='<%# (int)Eval("Taxes") == 1 %>'>
                  </asp:CheckBox>
        </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" />
     </asp:TemplateField>
      <%--Registrations--%>
       <asp:TemplateField HeaderText="REG" SortExpression="Registrations">
           <EditItemTemplate>
               <asp:CheckBox ID="cbRegEdit" runat="server" Checked='<%# (int)Eval("Registrations") == 1 %>' />
           </EditItemTemplate>
      <ItemTemplate>
               <asp:CheckBox ID="cbReg" runat="server" Enabled="false" Checked='<%# (int)Eval("Registrations") == 1 %>'>
                </asp:CheckBox>
       </ItemTemplate>
       <ItemStyle HorizontalAlign="Center" />
      </asp:TemplateField>

       <asp:TemplateField HeaderText="BTRColumn" SortExpression="BTR">
       <EditItemTemplate>
                <asp:CheckBox ID="cbBTREdit" runat="server" Checked='<%# (int)Eval("BTR") == 1 %>' />
        </EditItemTemplate>
       <ItemTemplate>
               <asp:CheckBox ID="cbBTR" runat="server" Enabled="false" Checked='<%# (int)Eval("BTR") == 1 %>'>
               </asp:CheckBox>
        </ItemTemplate>
      <ItemStyle HorizontalAlign="Center" />
     </asp:TemplateField>

Posted it as a comment but meant to post it as an answer haha! Been a long day.

I would loop through the check boxes and gather the IDs for them in a list. Lets call list "CheckBoxIDs". I would then do this: CheckBox cbox = (CheckBox)gvStations.Rows[i].FindControl(CheckBoxIDs[j]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM