Glory Raj
Glory Raj

Reputation: 17701

get the corresponding row values for check box checked row in grid view using javacsript

I have following grid with check boxes on each row, i want to get the corresponding selected row cells value when i checked the check box .. I am able to give the alert when the check box is checked but not sure how to get the selected row cells value ..

this is my gridview

  <asp:GridView ID="gvPRCertInfo" runat="server" AutoGenerateColumns="False" 
      GridLines="None"                      
      CellSpacing="1" CellPadding="1"
      Width="95%" BorderWidth="0"
      AllowSorting="True"
      PageSize="30"
      OnRowDataBound="gvPRCertInfo_RowDataBound"
      CssClass="data responsive">
      <Columns>
         <asp:TemplateField HeaderText="Select" SortExpression="">
            <HeaderTemplate>
               <asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true"  OnCheckedChanged="chkboxSelectAll_CheckedChanged"/>
            </HeaderTemplate>
            <ItemTemplate>
               <asp:CheckBox ID="chkCert" AutoPostBack="true" OnCheckedChanged="chkCert_CheckedChanged" OnClick="checkForVirtual(this);" runat="server" />
               <input type="hidden" id="hdnCertId" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "CertId") %>' />
            </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField DataField="CertificateID" HeaderText="Certificate ID" HeaderStyle-HorizontalAlign="Center" />
         <asp:BoundField DataField="partID" HeaderText="Part Number" HeaderStyle-HorizontalAlign="Center" />
         <asp:BoundField DataField="PartDesc" HeaderText="Part Description" HeaderStyle-HorizontalAlign="Center" />
         <asp:BoundField DataField="platformType" HeaderText="platformType" Visible="false" />

 ...................................
 ...................................

      </Columns>
      <EmptyDataRowStyle CssClass="AlternatingRowStyle" />     
  </asp:GridView>

And this is my checkbox javascript function

  function checkForVirtual(checkBox){
    if(checkBox.checked)
    {
        alert('checked');

       /// here i need to get selected checked row all cells value 
    }
 }

Could any one please suggest any ideas on this that would be very grateful to me..

many thanks in advance..

Upvotes: 0

Views: 1657

Answers (3)

user8905879
user8905879

Reputation:

 <HeaderTemplate>
 <asp:CheckBox ID="chkboxSelectAll"  AutoPostBack="true" OnCheckedChanged="chkboxSelectAll_CheckedChanged" runat="server" Text="All" />
 </HeaderTemplate>

 <ItemTemplate>
 <asp:CheckBox ID="chkCert" runat="server" Font-Size="XX-Small" />                                     
 </ItemTemplate>  

 </asp:TemplateField>

in cs file

protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chckheader = (CheckBox)gvPRCertInfo.HeaderRow.FindControl("chkboxSelectAll");
            foreach (GridViewRow row in gvPRCertInfo.Rows)
            {
                CheckBox CheckBoxchckrw = (CheckBox)row.FindControl("chkCert");
                if (chckheader.Checked == true)
                {
                    CheckBoxchckrw.Checked = true;
                }
                else
                {
                    CheckBoxchckrw.Checked = false;
                }
            }
        }

get details on button click

protected void button_Click(object sender, EventArgs e)
        {
            try
            {
                int cnt = 0;

                DataTable dt = new DataTable();
                dt.Columns.Add("Certificate ID");
                dt.Columns.Add("Part Number");
                dt.Columns.Add("Part Description");


                foreach (GridViewRow row in gvPRCertInfo.Rows)
                {
                    if ((row.Cells[0].FindControl("chkCert") as CheckBox).Checked)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Certificate ID"] = row.Cells[1].Text;
                        dr["Part Number"] = row.Cells[2].Text;
                        dr["Part Description"] = row.Cells[3].Text;

                        dt.Rows.Add(dr);
                        cnt++;
                    }
                }
                if (cnt <= 0)
                {
                    DataRow dr = dt.NewRow();
                    dr["Certificate ID"] = gvPRCertInfo.SelectedRow.Cells[1].Text;
                    dr["Part Number"] = gvPRCertInfo.SelectedRow.Cells[2].Text;
                    dr["Part Description"] = gvPRCertInfo.SelectedRow.Cells[3].Text;

                    dt.Rows.Add(dr);

                }

                 Session["Details"] = dt;
                Response.Redirect("url");
            }
            catch (Exception ex)
            {

            }
        }

Upvotes: 0

Darren Wood
Darren Wood

Reputation: 1528

Try this.

function checkForVirtual(checkBox){
    if(checkBox.checked)
    {
        var row = checkBox.parent.parent;
        row.getElementById('<%= hdnCertId.ClientID %>');// this will get the hdnCertId
        row.cell[0].getElementById('<%= hdnCertId.ClientID %>');
        alert('checked');
}

}

This might help

Upvotes: 0

VDWWD
VDWWD

Reputation: 35514

You can do this. It attaches a function to the checkbox change, finds the nearest tr and loops all the td

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= gvPRCertInfo.ClientID %> input[type="checkbox"]').change(function () {
            $(this).closest('tr').children('td').each(function () {
                alert($(this).html());
            });
        });
    });
</script>

However you have an AutoPostBack="true" in your checkbox, so everything you do in javascript is lost immediately due to a PostBack.

Upvotes: 1

Related Questions