I have a GridView with alternating row colors. I want to be able to change the row color onmouseover and then change it back to the original color onmouseout. I also want to be able to click anywhere on a row and have the whole row highlighted, then if I
click on a different row, I need the highlighted row to return to the original color and the new row to be highlighted. I have been somewhat successful with my attempt. Right now I can highlight the row onmouseover and change it back onmouseout, but I cant
figure out how to get it to tell the difference between alternating row colors, I can only get one color to show up onmouseout so when a row isnt highlighted anymore the alternating color gets wiped out. I don't know where to begin for the click process of
the row. Here is my code:
Protected Sub GridView_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGrid.RowCreated
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';")
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';")
End Sub
Thanks, that helps with the alternating row colors, but now it changes the color of my header row as well. Any idea how to stop the header from changing colors? I still have to find a solution so I can click on the rows and select them and unselect the row
when another is clicked on. Here is my revised code (You sent me the code in C# but I was able to convert it to VB.) :
Protected Sub GridView_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowCreated
Dim RowValue As Decimal
RowValue = Decimal.Remainder(e.Row.RowIndex, 2)
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';")
If RowValue = 0 Then
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';")
Else
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';")
End If
End Sub
I found some information on adding the onclick portion of the gridview, but I get an error and don't understand what I have to do to fix it. This code is supposed to use the select link and make the whole row clickable. The code I found is for a DataGrid
and I tried to convert it, but ran into the error. I added EnableEventValidation="true"
to the page but that did not fix t. Here is the code for the selecting rows onclick:
Private Sub MapGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowDataBound
Dim itemType As ListItemType = e.Row.RowType
If ((itemType = ListItemType.Pager) Or (itemType = ListItemType.Header) Or (itemType = ListItemType.Footer)) Then
Return
Else
Dim button As LinkButton = CType(e.Row.Cells(17).Controls(2), LinkButton)
e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(button, "")
End If
End Sub
Here is the error I recieve when I click on a row:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback
or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
OK, I fixed the issue with the header getting highlighted. Here is the code:
Protected Sub GridView_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowCreated
Dim RowValue As Decimal
RowValue = Decimal.Remainder(e.Row.RowIndex, 2)
If Not e.Row.RowIndex = -1 Then
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';")
End If
If RowValue = 0 And Not e.Row.RowIndex = -1 Then
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';")
ElseIf Not RowValue = 0 And Not e.Row.RowIndex = -1 Then
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';")
End If
End Sub
I still have the issue with selecting individual rows using onclick. Can anyone help me out there?
I found some information on adding the onclick portion of the gridview, but I get an error and don't understand what I have to do to fix it. This code is supposed to use the select link and make the whole row clickable. The code I found is for a DataGrid
and I tried to convert it, but ran into the error. I added EnableEventValidation="true"
to the page but that did not fix t. Here is the code for the selecting rows onclick:
Private Sub MapGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowDataBound
Dim itemType As ListItemType = e.Row.RowType
If ((itemType = ListItemType.Pager) Or (itemType = ListItemType.Header) Or (itemType = ListItemType.Footer)) Then
Return
Else
Dim button As LinkButton = CType(e.Row.Cells(17).Controls(2), LinkButton)
e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(button, "")
End If
End Sub
Here is the error I recieve when I click on a row:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback
or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
If anyone can help me fix this (or if someone has another way to do this) I would really appreciate the help.
mike_eps
Member
90 Points
18 Posts
Highlighting rows in GridView
Jul 13, 2006 01:00 PM|LINK
I have a GridView with alternating row colors. I want to be able to change the row color onmouseover and then change it back to the original color onmouseout. I also want to be able to click anywhere on a row and have the whole row highlighted, then if I click on a different row, I need the highlighted row to return to the original color and the new row to be highlighted. I have been somewhat successful with my attempt. Right now I can highlight the row onmouseover and change it back onmouseout, but I cant figure out how to get it to tell the difference between alternating row colors, I can only get one color to show up onmouseout so when a row isnt highlighted anymore the alternating color gets wiped out. I don't know where to begin for the click process of the row. Here is my code:
Protected Sub GridView_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGrid.RowCreated e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';") e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';") End SubCan anyone help me out with this?
Thanks,
Mike
AndokoChandr...
Participant
1010 Points
198 Posts
Re: Highlighting rows in GridView
Jul 13, 2006 02:04 PM|LINK
Why don't you use e.Row.RowIndex to filter whether a Row is alternating row or not, for example:
protected void GridView1_RowCreated ( object sender, GridViewRowEventArgs e )
{
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';");
if ( e.Row.RowIndex % 2 == 0 )
{
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';");
}
else
{
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#c0c0c0';");
}
}
Hope this helps
mike_eps
Member
90 Points
18 Posts
Re: Highlighting rows in GridView
Jul 13, 2006 02:32 PM|LINK
Thanks, that helps with the alternating row colors, but now it changes the color of my header row as well. Any idea how to stop the header from changing colors? I still have to find a solution so I can click on the rows and select them and unselect the row when another is clicked on. Here is my revised code (You sent me the code in C# but I was able to convert it to VB.) :
Protected Sub GridView_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowCreated Dim RowValue As Decimal RowValue = Decimal.Remainder(e.Row.RowIndex, 2) e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';") If RowValue = 0 Then e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';") Else e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';") End If End SubThanks.mike_eps
Member
90 Points
18 Posts
Re: Highlighting rows in GridView
Jul 13, 2006 02:57 PM|LINK
I found some information on adding the onclick portion of the gridview, but I get an error and don't understand what I have to do to fix it. This code is supposed to use the select link and make the whole row clickable. The code I found is for a DataGrid and I tried to convert it, but ran into the error. I added EnableEventValidation="true" to the page but that did not fix t. Here is the code for the selecting rows onclick:
Private Sub MapGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowDataBound Dim itemType As ListItemType = e.Row.RowType If ((itemType = ListItemType.Pager) Or (itemType = ListItemType.Header) Or (itemType = ListItemType.Footer)) Then Return Else Dim button As LinkButton = CType(e.Row.Cells(17).Controls(2), LinkButton) e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(button, "") End If End SubHere is the error I recieve when I click on a row:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Thanks.
mike_eps
Member
90 Points
18 Posts
Re: Highlighting rows in GridView
Jul 13, 2006 03:09 PM|LINK
OK, I fixed the issue with the header getting highlighted. Here is the code:
Protected Sub GridView_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowCreated Dim RowValue As Decimal RowValue = Decimal.Remainder(e.Row.RowIndex, 2) If Not e.Row.RowIndex = -1 Then e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightsteelblue';") End If If RowValue = 0 And Not e.Row.RowIndex = -1 Then e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#FFFFFF';") ElseIf Not RowValue = 0 And Not e.Row.RowIndex = -1 Then e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';") End If End SubI still have the issue with selecting individual rows using onclick. Can anyone help me out there?
Thanks.
mike_eps
Member
90 Points
18 Posts
Re: Highlighting rows in GridView
Jul 13, 2006 06:14 PM|LINK
I found some information on adding the onclick portion of the gridview, but I get an error and don't understand what I have to do to fix it. This code is supposed to use the select link and make the whole row clickable. The code I found is for a DataGrid and I tried to convert it, but ran into the error. I added EnableEventValidation="true" to the page but that did not fix t. Here is the code for the selecting rows onclick:
Private Sub MapGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MapGrid.RowDataBound Dim itemType As ListItemType = e.Row.RowType If ((itemType = ListItemType.Pager) Or (itemType = ListItemType.Header) Or (itemType = ListItemType.Footer)) Then Return Else Dim button As LinkButton = CType(e.Row.Cells(17).Controls(2), LinkButton) e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(button, "") End If End SubHere is the error I recieve when I click on a row:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
If anyone can help me fix this (or if someone has another way to do this) I would really appreciate the help.
Thanks.
</div>XAos
Member
14 Points
4 Posts
Re: Highlighting rows in GridView
Aug 03, 2006 03:57 PM|LINK
try this article; http://www.codeproject.com/aspnet/GridViewClientPostBack.asp
shajupnr
Member
2 Points
2 Posts
Re: Highlighting rows in GridView
Feb 28, 2008 03:36 PM|LINK
hai i tryed with u r code but no avail again shows same messge.plse have a help.
hallz
Member
2 Points
1 Post
Re: Highlighting rows in GridView
Jun 18, 2008 05:21 AM|LINK
rajuls
Member
20 Points
9 Posts
Re: Highlighting rows in GridView
Jun 23, 2008 11:19 AM|LINK
Simple and easy way to Highlight the row of gridview by clicking on the selected row...
add the following event on code behind page(.cs page)
Step 1:
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "HilightRow(this)");
//e.Row.Attributes.Add("onmouseover", "HilightRow1(this)"); for onmouseover of the row //e.Row.Attributes.Add("onmouseout", "HilightRow2(this)");
}
}
Step 2:
Add this event of gridview on aspx page, as shown below
<
asp:GridView ID="gvwDetails" runat="server"Style="position: relative; color: #000000; font-size: 12px;"
AutoGenerateColumns="False"
AllowSorting="True"
onsorting="OnSort" EmptyDataText="No Record Found"OnRowCreated="OnRowCreated"
BackColor="#D2EEFF" Width="1560">
<Columns>
.......
Step 3:
Add the javascript function on the aspx page as shown below
<script type="text/javascript" > var curSelRow = null;function HilightRow(rowid){
var selRow = rowid;
if (curSelRow != null)
{
curSelRow.style.backgroundColor = '#D2EEFF';
}
if(selRow != null)
{
curSelRow=selRow;
curSelRow.style.backgroundColor = 'Aqua';
}
}
//On mouseover
//function HilightRow1(rowid)
//{
//rowid.style.backgroundColor = 'Aqua';
//}
//onmouseout
//function HilightRow2(rowid)
//{
//rowid.style.backgroundColor = '#D2EEFF';
//}
</script>
Njoy !!! coding........
Rajul Saxena
B.Tech(Electronics)
Application Programmer