Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I had a aspx page where I created a Table Dynamically and filled it with data from a data table
and I had placed a check box as the first column in the table so that anybody who want to select the row can check the checkbox.For that I had created a Event also for the Checkbox control added to the
Now my issue is I want to get the row index of the checkbox being clicked .I had tried a number of steps including getting by elemnent ID but failed.Can anyone suggest a better method

C#
public void tablesetup()
   {
       DataTable dt = GetDataForApproval();
       TableHeaderRow header = new TableHeaderRow(); // Creating a header row
       Table1.Rows.Add(header);

       TableHeaderCell thdrcell = new TableHeaderCell();
       thdrcell.Text = "Select";
       header.Cells.Add(thdrcell);

       TableHeaderCell thdrcell23 = new TableHeaderCell();
       thdrcell23.Text = "CourierID";
       header.Cells.Add(thdrcell23);

       TableHeaderCell thdrcell145 = new TableHeaderCell();
       thdrcell145.Text = "Courier Date";
       header.Cells.Add(thdrcell145);

       TableHeaderCell thdrcell2 = new TableHeaderCell();
       thdrcell2.Text = "Courier Type";
       header.Cells.Add(thdrcell2);

       TableHeaderCell thdrcell3 = new TableHeaderCell();
       thdrcell3.Text = "Buyer";
       header.Cells.Add(thdrcell3);

       TableHeaderCell thdrcell4 = new TableHeaderCell();
       thdrcell4.Text = "Atc Or style";
       header.Cells.Add(thdrcell4);

       TableHeaderCell thdrcell5 = new TableHeaderCell();
       thdrcell5.Text = "Sender";
       header.Cells.Add(thdrcell5);

       TableHeaderCell thdrcell6 = new TableHeaderCell();
       thdrcell6.Text = "REciever";
       header.Cells.Add(thdrcell6);

       TableHeaderCell thdrcell7 = new TableHeaderCell();
       thdrcell7.Text = "Destination";
       header.Cells.Add(thdrcell7);

       TableHeaderCell thdrcell8 = new TableHeaderCell();
       thdrcell8.Text = "Atraco Account";
       header.Cells.Add(thdrcell8);

       TableHeaderCell thdrcel9 = new TableHeaderCell();
       thdrcel9.Text = "Approx.Cost";
       header.Cells.Add(thdrcel9);

       TableHeaderCell thdrcel10 = new TableHeaderCell();
       thdrcel10.Text = "Weight";
       header.Cells.Add(thdrcel10);

       if (dt.Rows.Count != 0)
       {
           int rowscount = dt.Rows.Count;
           int columncount = dt.Columns.Count;

           for (int i = 0; i < rowscount; i++)
           {
               TableRow tRow = new TableRow();
               Table1.Rows.Add(tRow);

               for (int j = 0; j < columncount; j++)
               {
                   TableCell tCell = new TableCell();
                   tRow.Cells.Add(tCell);
                   if (j == 0)
                   {
                       CheckBox bx = new CheckBox();
                       bx.AutoPostBack = true;

                       bx.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
                       tCell.Controls.Add(bx);
                   }
                   else
                   {
                       string data = dt.Rows[i][j - 1].ToString();

                       if (j == 1)
                       {
                           LinkButton link = new LinkButton();

                           link.Text = data;
                           link.Click += new System.EventHandler(lnk_Click);
                           tCell.Controls.Add(link);
                       }
                       else
                       {
                           tCell.Controls.Add(new LiteralControl(data));
                       }
                   }

               }

           }
       }
   }
   protected void CheckBox_CheckedChanged(object sender, EventArgs e)
   {
   }
Posted

1 solution

You may set the id of the checkbox which you have created to the following

bx.ID = "ChkBox_" + i.ToString();

and in your

C#
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
        CheckBox chkbox = (CheckBox)sender;
        string chkboxName = chkbox.ID.Split('_')[1];
        Response.Write(chkboxName);
}


This has worked for me I not sure if this the solution you require.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900