Question : Gridview RowSelected - Controlling state of other rows

Hi All,

I have the following grivdiew and RowSelected event.

Currently what happens is that the gridview is populated with a maximum of 3 records with an ImageButton control that when it is clicked it's image URL is changed so that it looks like a selected state.

What I need to achieve is that when the selected row's ImageButton has been selected, the other 2 row's Image URLs change to their unselected state (their original image source).

For example..

if the selected row's button has the image URL of '/images_core/button_subscribe_1.png' then this button changes to it's selected image state and the other row's buttons within the their image URL need to be:

/images_core/button_subscribe_2.png
/images_core/button_subscribe_3.png

Does anyone know how I can achieve this?

Many thanks,

Rit
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
<asp:GridView ID="GridViewPrices" runat="server" CssClass="table_prices" AutoGenerateColumns="False"
                                OnSelectedIndexChanged="GridViewPrices_RowSelected">
                                <Columns>
                                    <asp:TemplateField HeaderText="Price">
                                        <ItemTemplate>
                                            <strong>
                                                <asp:Literal ID="litPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Literal></strong>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Select Option">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="btnSubscribe" ImageUrl='<%# "/images_core/button_subscribe_" + Eval("DurationID") + ".png"  %>'
                                                runat="server" CommandName="Select" CausesValidation="False" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>





protected void GridViewPrices_RowSelected(object sender, EventArgs e)
{

        ImageButton btn = (ImageButton)GridViewPrices.SelectedRow.Cells[1].FindControl("btnSubscribe");

        switch (btn.ImageUrl)
        {
            case "/images_core/button_subscribe_1.png":
                btn.ImageUrl = "/images_core/button_subscribe_1selected.png";
                break;
            case "/images_core/button_subscribe_2.png":
                btn.ImageUrl = "/images_core/button_subscribe_2selected.png";
                break;
            case "/images_core/button_subscribe_3.png":
                btn.ImageUrl = "/images_core/button_subscribe_3selected.png";
                break;
        }
}

Answer : Gridview RowSelected - Controlling state of other rows

use something like this

  Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            e.Row.Attributes("onMouseOver") = "this.className='RSHover'"
            If e.Row.RowIndex Mod 2 = 0 Then
                e.Row.Attributes("onMouseOut") = "this.className='GridRowStyle'"
            Else
                e.Row.Attributes("onMouseOut") = "this.className='GridAlternatingRowStyle'"
            End If
        End If


    End Sub

where RSHover is a css class
Random Solutions  
 
programming4us programming4us