【问题标题】:dynamically match textbox width with gridview动态匹配文本框宽度与gridview
【发布时间】:2013-08-01 22:47:31
【问题描述】:

我正在尝试动态调整多个文本框的大小,以使它们与我的 gridviews 表头宽度相匹配。 gridview 将始终具有相同数量的列,但它们的宽度可能会有所不同。正如您在图像上看到的,宽度值根本不匹配。

值来自:

  • 黑色 = 代码宽度
  • 红色 = 通过检查元素 (firefox) 确定的文本框宽度
  • 蓝色 = 通过检查元素 (firefox) 确定的表头宽度

这是脚本和样式:

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%= myGridView.ClientID %> th").each(function (index) {
            $('input[type="text"]:eq(' + index + ')').css('width', $(this).width());
            $('input[type="text"]:eq(' + index + ')').css('padding', '0');
            $('input[type="text"]:eq(' + index + ')').val($(this).width());
        });
    });
</script>
<style type="text/css">
    input[type="text"] {
        margin: 0;
    }
</style>

根据要求,我的 ASP 代码

<input type="text" id="id0"/><!-- Comments are needed to get rid of whitespace between text boxes
         --><input type="text" id="id1"/><!--
         --><input type="text" id="id2"/><!--
         --><input type="text" id="id3" /><!--
         --><input type="text" id="id4" /><!--
         --><input type="text" id="id5" /><!--
         --><input type="text" id="id6" />
            <br />
            <asp:GridView ID="myGridView" runat="server" 
                HeaderStyle-CssClass="GridHeader" PagerStyle-CssClass="GridPager" FooterStyle-CssClass="GridFooter" 
                AlternatingRowStyle-CssClass ="GridAltItem" CssClass="Grid" ShowFooter="True" 
                AutoGenerateColumns="false" AllowSorting="True" DataSourceID="myDataSource" 
                OnRowUpdating="myGridView_RowUpdating">
                <Columns>
                    <asp:BoundField DataField="1" HeaderText="1" SortExpression="1" ReadOnly="True" />
                    <asp:BoundField DataField="2" HeaderText="2" SortExpression="2" />
                    <asp:BoundField DataField="3" HeaderText="3" SortExpression="3" />
                    <asp:BoundField DataField="4" HeaderText="4" SortExpression="4" />
                    <asp:BoundField DataField="5" HeaderText="5" SortExpression="5" />
                    <asp:BoundField DataField="6" HeaderText="6" SortExpression="6" />
                    <asp:BoundField DataField="7" HeaderText="7" SortExpression="7" />
                    <asp:commandfield showeditbutton="True" />
                    <asp:commandfield showdeletebutton="True" />
                </Columns>
                <FooterStyle CssClass="GridFooter"></FooterStyle>
                <PagerStyle CssClass="GridPager"></PagerStyle>
                <HeaderStyle CssClass="GridHeader"></HeaderStyle>
                <AlternatingRowStyle CssClass="GridAltItem"></AlternatingRowStyle>
            </asp:GridView>

【问题讨论】:

    标签: javascript jquery asp.net css gridview


    【解决方案1】:

    我相信仅此就足够了

     input[type="text"] {
        margin: 0;
        width: 100%;
    }
    

    【讨论】:

    • 您在删除 jquery 代码后尝试过吗?您的 gridview thtd 对于同一列具有相同的宽度(不同的列可能不同)?
    • 也尝试将选择器更改为$("#"+&lt;%= myGridView.ClientID %&gt;+" th").
    • 顺便说一句,您是在 gridview 上方的单独表格中添加文本框吗??
    • 我现在将 gridview 代码 + 文本框代码添加到我的问题中
    • 只是检查一下,能不能去掉gridview的边框,看看文本框是否排成一行?
    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多