【问题标题】:Resize an asp.net panel width based on a checkboxlist?根据复选框列表调整 asp.net 面板宽度?
【发布时间】:2010-05-05 18:02:24
【问题描述】:

我有一个包含复选框列表的 asp.net 面板。我想调整它的大小,使其宽度紧贴列表的内容。

现在我正在处理面板的预渲染事件,将其宽度设置为与复选框列表的宽度匹配。但是,checkboxlist 的 width 属性似乎为零(至少在此预渲染方法中),因此面板的宽度设置相同,这导致 Firefox 与 IE 中的渲染不一致。有没有人有更好的方法来做我在这里尝试的事情?非常感谢。

【问题讨论】:

    标签: asp.net panel checkboxlist


    【解决方案1】:

    您可以根据 javascript 设置面板宽度 onload:

    <script>
    function resizePanel() {
                var panel = document.getElementById('Panel1');
                var checkBox = document.getElementById('CheckBoxList1')
                if (panel != null && checkBox != null)
                    panel.style.width = checkBox.offsetWidth + "px";
    }
    </script>
    </head>
    <body onload="resizePanel()">
        <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server" BorderWidth="1" BorderColor="Silver">
        <asp:CheckBoxList ID="CheckBoxList1" Width="200px" runat="server">
        <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
        <asp:ListItem Text="Item 4" Value="4"></asp:ListItem>
        <asp:ListItem Text="Item 5" Value="5"></asp:ListItem>
        <asp:ListItem Text="Item 6" Value="6"></asp:ListItem>
        <asp:ListItem Text="Item 7" Value="7"></asp:ListItem>
        </asp:CheckBoxList>
        </asp:Panel>
        </form>
    </body>
    </html>
    

    【讨论】:

    • 感谢您的帮助。这对我有用,我必须检查 offsetWidth 是否大于(例如)25 像素的修改。否则,当复选框列表中没有数据时,偏移宽度为零,并且由于某种原因,我的面板在 IE 和 Firefox 中占据了整个屏幕。
    猜你喜欢
    • 2014-06-11
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    相关资源
    最近更新 更多