如何获取动态HTML表中所选复选框的TableRow索引 [英] How to get the TableRow index of the selected checkbox in dynamic HTML table

查看:114
本文介绍了如何获取动态HTML表中所选复选框的TableRow索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个aspx页面,我动态地创建了一个表,并用数据表中的数据填充它

我已经放置了一个复选框作为表中的第一列,以便任何想要的人选择行可以选中复选框。为此我创建了一个事件也为Checkbox控件添加到

现在我的问题是我想得到复选框的行索引被点击了。我尝试了很多步骤,包括获取elemnent ID但失败了。任何人都可以建议更好的方法



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

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)
   {
   }

推荐答案

你可以将您创建的复选框的ID设置为以下



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



和你的



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

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

and in your

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.

这篇关于如何获取动态HTML表中所选复选框的TableRow索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆