0

要绘制一种地图,我有一个表格,需要在每个单元格中动态添加一个 ImageButton。到目前为止,没有问题。但是,当单击其中一个时,我必须更改所有控件的鼠标悬停光标。更准确地说,我希望光标变成被点击的 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>
4

0 回答 0