Hi gabylopez,
Check this example. Now please take its reference and correct your code.
HTML
<asp:HiddenField ID="hididTitulo" runat="server" />
<asp:GridView ID="gvListaTit" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" GridLines="Both"
HorizontalAlign="Center" EmptyDataText="No se han encontrado resultados." AllowPaging="True" OnRowDataBound="gvListaTit_RowDataBound"
OnPageIndexChanging="gvListaTit_PageIndexChanging" PageSize="3" CssClass="table table-bordered">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderStyle-Width="65px">
<ItemTemplate>
<asp:ImageButton ID="btnSeleccionar" runat="server" CssClass="btn btn-simple btn-accion" ImageUrl="~/Imagenes/Iconos/check_24.png" OnClick="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvListaTit.DataSource = dt;
gvListaTit.DataBind();
}
protected void Edit(object sender, EventArgs e)
{
foreach (GridViewRow row1 in gvListaTit.Rows)
{
row1.BackColor = System.Drawing.Color.White;
}
ImageButton btn = sender as ImageButton;
GridViewRow row = btn.NamingContainer as GridViewRow;
hididTitulo.Value = row.Cells[0].Text;
row.BackColor = System.Drawing.ColorTranslator.FromHtml("#83CFC6");
}
protected void gvListaTit_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvListaTit.PageIndex = e.NewPageIndex;
BindGridView();
}
protected void gvListaTit_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.BackColor = System.Drawing.Color.White;
if (e.Row.Cells[0].Text == hididTitulo.Value)
{
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#83CFC6");
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvListaTit.DataSource = dt
gvListaTit.DataBind()
End Sub
Protected Sub Edit(ByVal sender As Object, ByVal e As EventArgs)
For Each row1 As GridViewRow In gvListaTit.Rows
row1.BackColor = System.Drawing.Color.White
Next
Dim btn As ImageButton = TryCast(sender, ImageButton)
Dim row As GridViewRow = TryCast(btn.NamingContainer, GridViewRow)
hididTitulo.Value = row.Cells(0).Text
row.BackColor = System.Drawing.ColorTranslator.FromHtml("#83CFC6")
End Sub
Protected Sub gvListaTit_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvListaTit.PageIndex = e.NewPageIndex
BindGridView()
End Sub
Protected Sub gvListaTit_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.BackColor = System.Drawing.Color.White
If e.Row.Cells(0).Text = hididTitulo.Value Then
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#83CFC6")
End If
End If
End Sub
Screenshot