Asp.net WebForm中應用Jquery EasyUI Layout

來源:互聯網
上載者:User
Asp.net WebForm中應用Jquery EasyUI Layout
按照EasyUI文檔中的樣本,編寫layout代碼:

<body class=”easyui-layout”>
        <div region="north" border="false" style="height:60px;background:#B3DFDA;">north region</div>
    <div region="west" split="true" title="West" style="width:150px;padding:10px;">west content</div>
    <div region="east" split="true" title="East" style="width:100px;padding:10px;">east region</div>
    <div region="south" border="false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
    <div region="center" title="Main Title">
    </div>
  </body>

在普通的HTML頁面中可以得到一個根據視窗大小自動調整的一個布局,可是放到Asp.net(.net 4)的webform中就會出錯:

<body class=”easyui-layout”>

<form id="form1" runat="server">
        <div region="north" border="false" style="height:60px;background:#B3DFDA;">north region</div>
    <div region="west" split="true" title="West" style="width:150px;padding:10px;">west content</div>
    <div region="east" split="true" title="East" style="width:100px;padding:10px;">east region</div>
    <div region="south" border="false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
    <div region="center" title="Main Title">
    </div>

</form>
  </body>

運行時顯示“nodename為空白或不是對象”

跟蹤調試發現 是因為定義布局的幾個DIV不是class定義為“easyui-layout”的元素的直接子物件。

於是將定義修改如下:

<form id="form1" runat="server" class=”easyui-layout”>

可是發現布局不能顯示,繼續研究發現是上面的form元素沒有定義絕對的寬度所至,如果想下面這樣定義就可以得到布局:

<form id="form1" runat="server" class=”easyui-layout”

style="width:600px;height:400px;">可是這樣得到的是一個固定大小的布局,不能夠隨著視窗大小改變尺寸。於是想到利用resize事件增加指令碼:<script type="text/javascript">
       

$(function () {
            windowResize();
            $(window).resize(function () {
                windowResize();
            });
        });

        function getWindowHeight() {
            return $(window).height();
        }
        function getWindowWidth() {
            return $(window).width();
        }
        function windowResize() {
            var width = getWindowWidth();
            var height = getWindowHeight();
            $('form#form1').width(width);
            $('form#form1').height(height);
            $('form#form1').layout();
        }

    </script>

 

<style type="text/css">
    body
    {
        padding:0px;
        margin:0px;
    }
    </style>
</head>
<body >
    <form id="form1" runat="server">
    <div region="north" border="false" style="height:60px;background:#B3DFDA;">north region</div>
    <div region="west" split="true" title="West" style="width:150px;padding:10px;">west content</div>
    <div region="east" split="true" title="East" style="width:100px;padding:10px;">east region</div>
    <div region="south" border="false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
    <div region="center" title="Main Title">
    </div>
    </form>
</body>

原來考慮將resize事件寫成一個獨立的函數,然後在$(window).resize(fn)中註冊,可是發現這樣一來,函數只會在表單第一次裝入時執行一次,以後不論怎麼改變視窗都不會再觸發事件,而直接將函數寫在resize()中,那麼表單裝載時又不執行!在IE、FireFox、Chorm中都是這樣,無奈只好這樣寫了!

通過上面的代碼能夠得到一個可以自適應視窗大小的布局!

ps:別忘了在<head>中加上必要的js檔案和CSS檔案的引用!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.