2017-06-19 73 views
0

即時通訊嘗試在我的imagebutton中創建一個onmouseover效果,它類似於下面這個css懸停效果代碼。onmouseover效果不在asp.net工作imagebutton

.button:hover 
{ 
    background-color: blue; 
} 

本來我有一個div標籤,其中一個類與CSS懸停效果相等,裏面是我的imagebutton。當我懸停圖像按鈕時,背景顏色藍色在圖像下方。這裏是下面的圖片:enter image description here

正如你從上面的圖片可以看到,當我懸停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"); 
    } 
} 


} 
+0

/:徘徊)? – mason

+0

Fyi..im真的是一個初學者,所以提供一個解決方案,這是高度讚賞 – RockStar

+0

我已經在網上搜索的答案,但沒有任何。我想知道有沒有人能解決這個問題。 – RockStar

回答

0

試試這個:

protected void getImageHover(object sender,RepeaterCommandEventArgs e) 
{ 
    ImageButton image = (ImageButton)e.CommandSource; 
    image.Attributes.Add("class","button"); 
} 

而在CSS,使用:

<style> 
.button 
{ 
    background-color: none; 
} 
.button:hover 
{ 
    background-color: blue; 
} 
</style> 
0

在Repeater的OnItemCommand事件中寫下面的代碼。希望這可你爲什麼要使用JavaScript來應用樣式,而不是CSS類和[懸停僞類(https://developer.mozilla.org/en-US/docs/Web/CSS幫助

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
ImageButton img = (ImageButton)e.Item.FindControl("brandImage"); 
image.Attributes.Add("onmouseover","this.style.backgroundColor=\"blue\""); 
}