2017-06-19 16 views
0

私のimagebuttonにonmouseoverエフェクトを作成しようとしていますが、これは以下のこのcssホバーエフェクトコードに似ています。onmouseoverエフェクトがasp.net imagebuttonで動作しない

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

元は、CSSのホバー効果と同じクラスのdivタグがあり、その内側にはイメージボタンがあります。イメージボタンをマウスで動かすと、背景色の青色が画像の下に表示されます。 enter image description here

上記の画像からわかるように、私はIbanezのロゴの上に表示されているように、左、右、下の境界線に青い線のみを作成します。だから私はホバー効果をimagebuttonに直接実装しようと決心しました。これは通常のCSSのアプローチよりも驚くほど困難です。私はコードビハインドでonmouseoverを追加するべきであることをオンラインで読んだことがあります。なぜなら、imagebuttonは通常それを持っておらず、Page_Loadに配置されるべきだからです。

私のイメージボタンは画像ボタンのIDに直接アクセスできないリピーターの中にあるので、私の場合はできません。だから、私はImageButtonにアクセスするためにOnItemCommandを作成しました。残念なことに私の解決策はうまくいかず、イメージ・ボタンでホバリングも起こっていません。この問題の解決に私を助けてください。ここで

は、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

このお試しください:CSSでいる間

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

を、使用:

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

以下のコードをRepeaterのOnItemCommandイベントに記述します。これはなぜあなたの代わりにCSSクラスのスタイリングと[ホバー擬似クラス](https://developer.mozilla.org/en-US/docs/Web/CSSを適用するにはJavaScriptを使用している

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\""); 
} 
関連する問題