0

我有一个 aspx 页面,该页面有一个如下表

<table id="tbl1" runat="server">
<tr>
<td>
Hello world
</td>
</tr>
</table>

现在我想从后面的代码(C#)中获取表格的位置(x,y 坐标)。

有没有办法得到它?

4

2 回答 2

2

试试这个

ASPX:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
        function getPosition(elmID) {
            var elmPosition = $("#"+elmID).position();
            $("#<%= hdnPosition.ClientID%>").val(elmPosition.left + "," + elmPosition.top);
        }

        $(document).ready(function () {
            $("#<%= btnGetPosition.ClientID %>").click(function () {
                getPosition("tbl1");
            });
        });
    </script>
</head>
<body>
    <form id="frmMain" runat="server">
    <table id="tbl1" runat="server">
        <tr>
            <td>
                Hello world
            </td>
        </tr>
    </table>
    <asp:HiddenField ID="hdnPosition" runat="server" />
    <asp:Button ID="btnGetPosition" runat="server" Text="Get position" 
        onclick="btnGetPosition_Click"/>
    </form>
</body>
</html>


代码隐藏:

protected void btnGetPosition_Click(object sender, EventArgs e)
{
    string position = hdnPosition.Value;
}  
于 2013-05-27T07:07:04.620 回答
1

您可以通过多种方式实现这一目标

  1. 添加一个占位符并在运行时放置您的表格:-

    <asp:placeholder id="PlaceHolder1" runat="server" xmlns:asp="#unknown">
    </asp:placeholder>
    
  2. 在运行时设置 CSS

    style1
    {
    position:absolute;
    left:100px; //Example
    top:150px; //Example
    }
    

    在 CS 代码中,使用

    Table.CssClass = "style1";
    
  3. 您可以使用 Control.Style 属性来设置控件的样式:

    Table tbl = new Table();
    tbl.Style[HtmlTextWriterStyle.Position] = "Absolute";
    tbl.Style[HtmlTextWriterStyle.Top] = "10px";
    
  4. 如果你想要它在完整的 JavaScript 中。例如:

    <!DOCTYPE html>
    <head>
    <title>Overlapping tables</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    
    <style type="text/css" media="all">
    table.first
    {
        position: absolute;
        margin: 0;
        padding: 0;
    
        color: #000000;
        background-color: #ff0000;
        text-align: center;
    }
    
    table.second
    {
        margin: 0;
        padding: 0;
        position: relative;
    
        text-align: center;
        color: #00ff00;
        background-color: transparent;
    }
    
    </style>
    
    <script type="text/javascript"><!--
    
    onload = setPos;
    
    function setPos ()
    {
        var table_1 = new namedTable ('first_table');
        var table_2 = new namedTable ('second_table');
    
        table_2.style.top = table_1.style.top - 1;
        table_2.style.left = table_1.style.left - 1;
    }
    
    
    function namedTable(name)
    {
    
        if (document.getElementById)
        {
            this.obj = document.getElementById(name);
            this.style = document.getElementById(name).style;
        }
        else if (document.all)
        {
            this.obj = document.all[name];
            this.style = document.all[name].style;
        }
        else if (document.layers)
        {
            this.obj = document.layers[name];
            this.style = document.layers[name];
        }
    }
    
    //--></script>
    
    </head>
    <body>
    
    <table class="first" id="first_table">
    <tr>
        <td>
            <h1>TABLE</h1>
        </td>
    </tr>
    </table>
    
    <table class="second" id="second_table">
    <tr>
        <td>
            <h1>TABLE</h1>
        </td>
    </tr>
    </table>
    
    </body>
    </html>
    

笔记:

1) 文档仍然在 w3.org 上验证(CSS 和 HTML)。

2) 在 Mozilla 和 Internet Explorer 中测试。

3) 由于习惯,我使用 doc-type xhtml1-strict.dtd,但在为 Mozilla 发布时不应使用此 doctype。改用 HTML 过渡 - 或者干脆不放标题(这将使浏览器在 Quirk 模式下运行,并且对脚本错误更宽容)。

4)如果您想进一步研究这个主题,这里是我所知道的最精确和最轻量级的 Javascript 定位技术的链接:

http://www.quirksmode.org/

祝你好运!

于 2013-05-27T07:41:37.960 回答