将列值作为id添加到asp.net中GridView的每一行
我使用这段代码为GridView的每一行提供唯一的id将列值作为id添加到asp.net中GridView的每一行,gridview,Gridview,我使用这段代码为GridView的每一行提供唯一的id protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType == DataControlRowType.DataRow) { row.Attributes["id"] = cRowID.ToString(); cRowID
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
row.Attributes["id"] = cRowID.ToString();
cRowID++;
}
}
其中cRowID是全局整数变量。这段代码为我提供了如下HTML代码
<table id="gridview1">
<tr id="1"><td>...</td></tr>
<tr id="2"><td>...</td></tr>
.
.
<tr id="n"><td>...</td></tr>
</table>
...
...
.
.
...
如何用添加到同一行的特定列(比如column1)值替换cRowID
编辑在HTML中添加tr标记的ID之后,我希望它变成
<tr id="abc" ><td>abc</td><td>...</td></tr>
<tr id="pqr" ><td>pqr</td><td>...</td></tr>
<tr id="xyz" ><td>xyz</td><td>...</td></tr>
abc。。。
pqr。。。
xyz。。。
可能吗?如果是,请解释如何测试?试试这个
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int counter=1;
foreach (GridViewRow oItem in GridView1.Rows)
{
counter++;
oItem.Cells[0].Text = counter.ToString();
oItem.Attributes.Add("id", "yourvalue");//here you can set row id ie(<tr>)
}
}
试试这个
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int counter=1;
foreach (GridViewRow oItem in GridView1.Rows)
{
counter++;
oItem.Cells[0].Text = counter.ToString();
oItem.Attributes.Add("id", "yourvalue");//here you can set row id ie(<tr>)
}
}
你不需要经历一个循环。下面给出的代码运行良好:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string getFirstColValue = e.Row.Cells[0].Text;
e.Row.Attributes.Add("id", getFirstColvalue );
}
}
你不需要经历一个循环。下面给出的代码运行良好:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string getFirstColValue = e.Row.Cells[0].Text;
e.Row.Attributes.Add("id", getFirstColvalue );
}
}
@PrasadJadhav:Sry我没有得到它,所以请更详细地解释它,您到底想做什么?在运行时,GridView在页面上呈现为HTML标记。此表标记具有多个标记。我想给这个tr标签一个ID。其Id的值(即)将是表中第一列的值(即第一列与该值之间的值)。Gridview在页面上呈现为HTML标记。因此,
标记用于行
,用于列
。因此,oItem.Cells[0]。Text
向oItem指示行,而Cells[0]指示第一列。:谢谢,它成功了!!!只是出于好奇,我们可以分配主键值而不是分配第一列值,这样我们分配的ID将是唯一的吗?@PrasadJadhav:是的,您可以设置唯一的值,您只需要分配字符串变量的值,然后添加属性id@PrasadJadhav:Sry我没弄明白,所以,请更详细地解释您到底想做什么?在运行时,GridView在页面上呈现为HTML标记。此表标记具有多个标记。我想给这个tr标签一个ID。其Id的值(即)将是表中第一列的值(即第一列与该值之间的值)。Gridview在页面上呈现为HTML标记。因此,标记用于行
,用于列
。因此,oItem.Cells[0]。Text
向oItem指示行,而Cells[0]指示第一列。:谢谢,它成功了!!!只是出于好奇,我们可以分配主键值而不是分配第一列值,这样我们分配的id将是唯一的吗?@PrasadJadhav:是的,您可以设置唯一的值,您只需要分配字符串变量的值,然后添加属性id