添加图像按钮鼠标悬停事件

时间:2011-01-31 15:53:46

标签: c# image events mouseover imagebutton

我有网页表单,我将图像(图像按钮)添加到表中,该表在运行时从数据库动态创建...有一个静态图像,它将根据动态图像鼠标改变...

这是代码:

HtmlTable tbProductImage = new HtmlTable();
                    HtmlTableRow trImageRow = new HtmlTableRow();
                    for (int j = 0; j < columnCount; j++) {
                        if (filteredFileList.Count != 0) {
                            HtmlTableCell tdImageRow = new HtmlTableCell();
                            Panel panel = new Panel();
                            ImageButton btnProduct = new ImageButton();                           
                            btnProduct.ID = "btn" + filteredFileList[j].Name.Substring(0, filteredFileList[j].Name.LastIndexOf("."));
                            btnProduct.ImageUrl = @"/ysyp/Images/Products/" + filteredFileList[j].Name; 
                            btnProduct.Width = 50;
                            btnProduct.CommandName = "Click";
                            Page.ClientScript.GetPostBackEventReference(btnProduct, "btnProduct_Click");
                            btnProduct.CommandArgument = filteredFileList[j].Name;
                            btnProduct.Click += new ImageClickEventHandler(btnProduct_Click);
                            panel.Controls.Add(btnProduct);
                            trImageRow.Cells.Add(tdImageRow);
                            tdImageRow.Controls.Add(panel);
                        }
                    }
                    tbProductImage.Rows.Add(trImageRow);
                    tdProduct.Controls.Add(tbProductImage);

我该怎么做......

谢谢......

2 个答案:

答案 0 :(得分:0)

使用CSS伪选择器hover

将类添加到图像按钮:

btnProduct.CssClass = "hoveredButton";

在css中定义 hoverButton 类:

hoveredButton:hover{background-image:url('path-to-your-image')}

答案 1 :(得分:0)

如果有人想要格式化的代码:

HtmlTable tbProductImage = new HtmlTable();
HtmlTableRow trImageRow = new HtmlTableRow();
for (int j = 0; j < columnCount; j++)
{
    if (filteredFileList.Count != 0)
    {
        HtmlTableCell tdImageRow = new HtmlTableCell();
        Panel panel = new Panel();
        ImageButton btnProduct = new ImageButton();

        btnProduct.ID = "btn" + filteredFileList[j].Name.Substring(0, filteredFileList[j].Name.LastIndexOf("."));
        btnProduct.ImageUrl = @"/ysyp/Images/Products/" + filteredFileList[j].Name;
        btnProduct.Width = 50;
        btnProduct.CommandName = "Click";

        Page.ClientScript.GetPostBackEventReference(btnProduct, "btnProduct_Click");

        btnProduct.CommandArgument = filteredFileList[j].Name;
        btnProduct.Click += new ImageClickEventHandler(btnProduct_Click);

        panel.Controls.Add(btnProduct);

        trImageRow.Cells.Add(tdImageRow);
        tdImageRow.Controls.Add(panel);
    }
}

tbProductImage.Rows.Add(trImageRow);
tdProduct.Controls.Add(tbProductImage);