即时通讯尝试在我的imagebutton中创建一个onmouseover效果,它类似于下面这个css悬停效果代码。onmouseover效果不在asp.net工作imagebutton
.button:hover
{
background-color: blue;
}
本来我有一个div标签,其中一个类与CSS悬停效果相等,里面是我的imagebutton。当我悬停图像按钮时,背景颜色蓝色在图像下方。这里是下面的图片:
正如你从上面的图片可以看到,当我悬停Ibanez标志,它只在左,右和底部边框创建一条蓝线。所以我决定尝试直接在imagebutton中实现悬停效果,这比正常的css方法令人惊讶的困难。我在线阅读到,您应该在代码隐藏中添加onmouseover,因为imagebutton通常不会将其添加到Page_Load中。
在我的情况下,我不能这样做,因为我的imagebutton是在一个中继器,它不能直接访问图像按钮的id。所以我最终做出了一个OnItemCommand来访问imagebutton。很不幸,我的解决方案无法工作,我的图像按钮中没有悬停。请帮我解决这个问题。
下面是ASPX:
<%@ Page Title="" Language='C#' MasterPageFile='~/MasterPage.master'
AutoEventWireup='true' CodeFile='GuitarBrands.aspx.cs'
Inherits='Pages_GuitarBrands' %>
<asp:Content ID='Content1' ContentPlaceHolderID='ContentPlaceHolder1'
Runat='Server'>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="getImageHover">
<ItemTemplate>
<div class="one-third">
<asp:ImageButton ID="brandImage" OnClick="Repeater1_OnClick"
height="250px" width="300px" runat="server" ImageUrl='<%# Eval("image")
%>' CommandArgument='<%# Eval("id") %>'
onmouseover="this.style.backgroundColor='blue';" />
</div>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
这里是aspx.cs代码:
public partial class Pages_GuitarBrands : System.Web.UI.Page
{
public List<guitarBrand> brandList { get; set; }
private string brandType = "Guitar";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
DataSet ds = GetData();
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
}
protected void getImageHover(object sender,RepeaterCommandEventArgs e)
{
ImageButton image = (ImageButton)e.CommandSource;
image.Attributes.Add("onmouseover","this.style.backgroundColor=\"blue\"");
}
private DataSet GetData()
{
string CS = ConfigurationManager.ConnectionStrings["brandsConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter("Select * from guitarBrands", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
protected void Repeater1_OnClick(object sender, EventArgs e)
{
ImageButton image = (ImageButton)sender;
if (image != null) {
int id = int.Parse(image.CommandArgument);
string brandName = ConnectionClassBrands.GetBrandById(id);
ConnectionClassGuitarItems.guitar = brandName;
Response.Redirect("~/Pages/GuitarItems1.aspx");
}
}
}
/:徘徊)? – mason
Fyi..im真的是一个初学者,所以提供一个解决方案,这是高度赞赏 – RockStar
我已经在网上搜索的答案,但没有任何。我想知道有没有人能解决这个问题。 – RockStar