简体   繁体   中英

How to get HTML portion of the selected row in the Gridview?

I've a gridview with checkbox in the first column, I need to get the html of the row portion of the selected row. what is a good way to do this?

Below portion logic is required to get the selected row html.

foreach (GridViewRow row in GridView1.Rows)
{
    if (!(row.FindControl("CheckBox1") as CheckBox).Checked)
    {
        //logic required here
    }
}

You can use this snippet to get the row as HTML in the GridView OnRowDataBound event.

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM