ASP.NET ImageButton控件上的自定义光标悬停

时间:2013-10-11 10:16:11

标签: asp.net image cursor imagebutton

要绘制一种地图,我有一个表,需要在每个单元格中动态添加一个ImageButton。到目前为止,没问题。但是当我点击其中一个时,我必须更改所有控件的onmouseover游标。更确切地说,我希望光标成为点击的ImageButton的ImageUrl,这是一种png格式。

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        // Create new row and add it to the table.
        TableRow line = new TableRow();
        _DomainMap.Rows.Add(line);
        for (int j = 0; j < 30; j++)
        {
            // Create a new cell and add it to the row.
            TableCell cell = new TableCell();
            cell.ID = "_Case"+i+j;
            ImageButton domain_item = new ImageButton();
            domain_item.CssClass = "domainitem";
            domain_item.ImageUrl = "~/Images/empty.png";
            domain_item.Click += new ImageClickEventHandler(_DomainItem_Click);
            domain_item.ID = "_Image" + i + j;
            cell.Controls.Add(domain_item);
            line.Cells.Add(cell);
        }
    }
}

protected void _DomainItem_Click(object sender, EventArgs e)
{
    ChangeCursor(((ImageButton)sender).ImageUrl);
}

protected void ChangeCursor(String cursor_path)
{
    for (int i = 0; i < _DomainMap.Rows.Count; i++)
    {
        for (int j = 0; j < _DomainMap.Rows[i].Cells.Count; j++)
        {
            ImageButton imgb = ((ImageButton)_DomainMap.FindControl("_Image" + i + j));
            imgb.Attributes.Add("onmouseover", "setcursor(" + imgb + ", " + imgb.ImageUrl + ");");
        }
    }
}

和“setcursor”javascript函数:

<script type="text/javascript">
    function setcursor(control, cur)
    {
        control.style.cursor = "url("+cur+")";
    }
</script>

0 个答案:

没有答案