Fellow coders, I have GridView that contains a HyperLinkField that looks like this:<asp:HyperLinkFieldHeaderText="Click For"Text="SubCategories"ItemStyle-VerticalAlign="top"ItemStyle-Width="100"DataNavigateUrlFields="CategoryD"DataNavigateUrlFormatString="EditCurrentCat.aspx?cid={0}"ControlStyle-CssClass="Menu"/>When you click on the link it'll populate a another gridview based on cid that is sent to it. When I click this link in the particular row,
I need to have that row in the "parent" gv to be highlighted and here is the code that I though would do it:if (gvCategories.SelectedIndex
== -1){ gvCategories.SelectedRowStyle.BackColor
= System.Drawing.Color.Green;}But
it doesn't work. When I step through the code, it'll go inside the "if" statement, but nothing happens. This "if" statement is located in the Page_Load method inside an 'if ( !ISPostBack )". Is the reason for not highlighting the row because the it is not
a true postback? If so can you offer an workaround?Thank you!
When you are clicking on the hyperlink field, you are not actually selecting
a row in the gridview.
Because of this, even though you are setting the selectedrowstyle backcolor, it is not being applied. To work around this problem you need to identify the row that was clicked and then set the backgroundcolor of that row.
Yes, thank you for that. That would make sense and I can totaly visualize it, except that I'm lacking the "how to". If you'd be so kind and did share the "how to", I'd really appreciate it a lot. Thanks.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Select" Then
GridView1.Rows(e.CommandArgument).BackColor = Drawing.Color.Green
End If
End Sub
e.commandargument is the index of the selected row
gridview1.DataKeys(e.CommandArgument) will give you the DataKey associated with the row
Thanks for that informaiton. I tried to translated it into C# but it didn't like the whole thing. In the "if" statement it errored out on "e" - said: "Property or indexer 'System.Web.UI.WebControls.CommandEventArgs.CommandName' cannot be assigned to -- it
is read only" and "Cannot implicitly convert type 'string' to 'bool' ".
For the statment inside the "if" GridView1 threw the following: "The best overloaded method match for 'System.Web.UI.WebControls.GridViewRowCollection.this[int]' has some invalid arguments" and for the "e" just after "GridView1.Rows" threw the following:
"Error 6 Argument '1': cannot convert from 'object' to 'int' ". (off course I was using my gv name not the one in this example).
Shouldn't there be a Command declaration in the in the <asp:GreidView ...> tag to this effect? And in your example the "if" statemet evaluates a Command Name="Select" but the command field has no such property and neither has the GridView declaration tag.
Is this automatically called a "Select", which seems strange, but than again "Bill" has his ways. Thank for your time and patience!
Thanks again for the reply. It is still however choking on the on the line inside the "if" statement: "The best overloaded method match for 'System.Web.UI.WebControls.GridViewRowCollection.this[int]' has some invalid arguments"; and "Argument '1': cannot
convert from 'object' to 'int' "
I made it work, sort of, but not really. I converted the e.CommandArgument in to an int which compiled ok. In the gv however, when clicked a row for a first time it did turn green, then I clicked on a diferent row which it turned green as but the previusly
selected row remeined green as well.
I made it work, sort of, but not really. I converted the e.CommandArgument in to an int which compiled ok. In the gv however, when clicked a row for a first time it did turn green, then I clicked on a diferent row which it turned green as but the previously
selected row remeined green as well.
EJM
Member
190 Points
415 Posts
Trouble with HyperLinkField inside the GridView
Aug 15, 2007 02:04 PM|LINK
EJM
Prashant Kum...
Star
12344 Points
1992 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 15, 2007 02:25 PM|LINK
When you are clicking on the hyperlink field, you are not actually selecting a row in the gridview.
Because of this, even though you are setting the selectedrowstyle backcolor, it is not being applied. To work around this problem you need to identify the row that was clicked and then set the backgroundcolor of that row.
EJM
Member
190 Points
415 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 15, 2007 02:30 PM|LINK
Yes, thank you for that. That would make sense and I can totaly visualize it, except that I'm lacking the "how to". If you'd be so kind and did share the "how to", I'd really appreciate it a lot. Thanks.
Prashant Kum...
Star
12344 Points
1992 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 15, 2007 03:26 PM|LINK
One way of doing it
Use a commandfield
<asp:CommandField ShowSelectButton="true" SelectText="Sub Categories" />
and in the rowcommand event
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Select" Then
GridView1.Rows(e.CommandArgument).BackColor = Drawing.Color.Green
End If
End Sub
e.commandargument is the index of the selected row
gridview1.DataKeys(e.CommandArgument) will give you the DataKey associated with the row
EJM
Member
190 Points
415 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 17, 2007 02:41 PM|LINK
Thanks for that informaiton. I tried to translated it into C# but it didn't like the whole thing. In the "if" statement it errored out on "e" - said: "Property or indexer 'System.Web.UI.WebControls.CommandEventArgs.CommandName' cannot be assigned to -- it is read only" and "Cannot implicitly convert type 'string' to 'bool' ".
For the statment inside the "if" GridView1 threw the following: "The best overloaded method match for 'System.Web.UI.WebControls.GridViewRowCollection.this[int]' has some invalid arguments" and for the "e" just after "GridView1.Rows" threw the following: "Error 6 Argument '1': cannot convert from 'object' to 'int' ". (off course I was using my gv name not the one in this example).
Shouldn't there be a Command declaration in the in the <asp:GreidView ...> tag to this effect? And in your example the "if" statemet evaluates a Command Name="Select" but the command field has no such property and neither has the GridView declaration tag. Is this automatically called a "Select", which seems strange, but than again "Bill" has his ways. Thank for your time and patience!
EJM
Prashant Kum...
Star
12344 Points
1992 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 17, 2007 04:26 PM|LINK
In C#
If (e.CommandName == "Select")
GridView1.Rows[e.CommandArgument].BackColor = Drawing.Color.Green;
EJM
Member
190 Points
415 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 17, 2007 05:46 PM|LINK
Thanks again for the reply. It is still however choking on the on the line inside the "if" statement: "The best overloaded method match for 'System.Web.UI.WebControls.GridViewRowCollection.this[int]' has some invalid arguments"; and "Argument '1': cannot convert from 'object' to 'int' "
EJM
Member
190 Points
415 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 17, 2007 06:14 PM|LINK
I made it work, sort of, but not really. I converted the e.CommandArgument in to an int which compiled ok. In the gv however, when clicked a row for a first time it did turn green, then I clicked on a diferent row which it turned green as but the previusly selected row remeined green as well.
EJM
Member
190 Points
415 Posts
Re: Trouble with HyperLinkField inside the GridView
Aug 17, 2007 06:15 PM|LINK
I made it work, sort of, but not really. I converted the e.CommandArgument in to an int which compiled ok. In the gv however, when clicked a row for a first time it did turn green, then I clicked on a diferent row which it turned green as but the previously selected row remeined green as well.