我可以在cs文件中成功突出显示鼠标悬停时的Gridview行,并且在没有javascript的情况下完全正常工作。但是,我无法在点击时选择并突出显示Gridview行。 谁能帮助我如何以最简单的方式选择并突出显示点击Gridview行?
这些是来自CS档案的代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='red';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
GridView1.SelectedRow.BackColor = System.Drawing.Color.Yellow;
}
这是我的aspx代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged">
<HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Black" />
<Columns>
<asp:TemplateField HeaderText="Toy_Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="100px" Width="150px"
ImageUrl='<%#"data:Image/png/jpg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Toy_Image")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Toy_Name" HeaderText="Toy_Name" />
<asp:TemplateField HeaderText="Anime_Image">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" Height="100px" Width="150px"
ImageUrl='<%#"data:Image/png/jpg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Anime_Image")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Anime_Name" HeaderText="Anime_Name" />
<asp:TemplateField HeaderText="Toy_Distributor_Image">
<ItemTemplate>
<asp:Image ID="Image3" runat="server" Height="100px" Width="150px"
ImageUrl='<%#"data:Image/png/jpg/gif/bmp;base64," + Convert.ToBase64String((byte[])Eval("Toy_Distributor_Image")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Toy_Distributor" HeaderText="Toy_Distributor" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>