使用jquery从c#中动态创建的html表中获取行值

时间:2018-02-16 11:34:31

标签: c# jquery

我试图从我在c#中动态生成的表中获取单元格值。在最后一栏中,我填写了下拉列表。在下拉列表的事件更改中想要从特定行获取值。此时,我在行之间无法区分,并且当触发更改侦听器时,将获取表中的所有值。

以下是我的代码

PendingFiles.cs

Table tbl = new Table();
tbl.ID = "PendingTable";
tbl.Rows.Add(new TableRow());
tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "Name";

tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "Address";

tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "Email Address";

foreach (var item in DC.refermember)
{
    DropDownList drplst = new DropDownList();
    drplst.ID = Guid.NewGuid().ToString();

    drplst.Items.Add("--Select--");
    drplst.Items.Add("APPROVE");
    drplst.Items.Add("STUDENT");
    drplst.Items.Add("ARTS/MEDIA");
    drplst.Items.Add("INCOMPLETE");
    drplst.Attributes.Add("onchange", "handleDropDownEvents(this);");
    //Panel1.Controls.Clear();

    tbl.Rows.Add(new TableRow());

    //tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableCell());
    //tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = item.ReferId;
    //tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Enabled = false;

    tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableCell());
    tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = item.FirstName + " " + item.LastName;

    tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableCell());
    tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = item.FirstName + " " + item.LastName;

    tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableCell());
    tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = item.HomeAddress;

    tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableCell());
    tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Controls.Add(drplst);

    Panel1.Controls.Add(tbl);
}

的.aspx

function handleDropDownEvents(e) {
    //alert(e.value)
    //if (e.value == "APPROVE") {
    //var $table = $("#ctl00_ContentPlaceHolder1_PendingTable").find("tr")

    var $row = $(this).parents('tr'); //Here I have to change something
    var email = $row.find('td:nth-child(4)').text();
    var firstname = $row.find('td:nth-child(2)').text();
}

1 个答案:

答案 0 :(得分:1)

你把这个'而不是'!我认为使用最接近的'会更好。

var $row = $(e).closest('tr');

另一个建议:

TableRow row = new TableRow();
row.Cells.Add(new TableHeaderCell() { Text = "Name" });
row.Cells.Add(new TableHeaderCell() { Text = "Address" });
row.Cells.Add(new TableHeaderCell() { Text = "Email Address" });
tbl.Rows.Add(row);