我在第一列中有一个带有复选框的gridview,我需要获取所选行的行部分的html。这样做的好方法是什么?
需要以下部分逻辑来获取所选行html。
foreach (GridViewRow row in GridView1.Rows)
{
if (!(row.FindControl("CheckBox1") as CheckBox).Checked)
{
//logic required here
}
}
答案 0 :(得分:3)
您可以使用此代码段在GridView OnRowDataBound
事件中将该行作为HTML获取。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
if (cb.Checked == true)
{
TableRow row = e.Row;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
row.RenderControl(htw);
string rowContents = sw.ToString();
}
}
}