[英]how to get dynamically created checkbox row value in gridview using javascript
我创建了带有多个动态生成的复选框的网格视图,但是所有复选框都具有相同的ID。 如何获取所选复选框的行值?
这是我在gridvie rowdatabound事件上的动态控件
protected void grdreport_RowDataBound(object sender, GridViewRowEventArgs e)
{
int temp = e.Row.Cells.Count;
temp--;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (temp >= 3)
{
strheadertext1 = grdreport.HeaderRow.Cells[3].Text;
CheckBox cb1 = new CheckBox();
cb1.ID = "cb1";
cb1.Text = e.Row.Cells[3].Text;
e.Row.Cells[3].Controls.Add(cb1);
}
}
}
而且我在单击按钮时就获得了价值
protected void BtnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdreport.Rows)
{
CheckBox checkbox1 = (CheckBox)row.FindControl("cb1");
checkbox1.Checked = true;
if (checkbox1.Checked)
{
string itemname = row.Cells[0].Text;
string particular = row.Cells[1].Text;
string qty = row.Cells[2].Text;
}
}
}
但是当我获得价值时,只要我选中第二行复选框,它就会给我第一行价值
您正在对ID进行硬编码。 做这样的事情:
//(or take the count of something else if this is not suitable)
int i = e.Row.Cells[3].Controls.count + 1;
CheckBox cb1 = new CheckBox();
cb1.ID = "cb" + i;
在JavaScript中查找元素:
var checkbox = document.getElementById("checkboxid");
您正在循环所有行,但是永远不要将值存储在循环外,因此这些值将始终是最后一个选中的复选框。 看看下面的代码片段和写入Label1
的值。 这些将是每行的值。
protected void BtnSave_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdreport.Rows.Count; i++)
{
GridViewRow row = grdreport.Rows[i];
CheckBox checkbox1 = (CheckBox)row.FindControl("cb1");
if (checkbox1.Checked)
{
Label1.Text += string.Format("Row {0}: {1}<br>", i, row.Cells[1].Text);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.