简体   繁体   English

ASP.NET如何使TextBox的宽度取决于GridView列的宽度?

[英]ASP.NET How to make TextBox width dependent on GridView column's width?

I need to make filters on table (GridView), on the top of each column should appear "TextBox" to enter filter values. 我需要在表(GridView)上创建过滤器,在每列的顶部应显示“ TextBox”以输入过滤器值。 GridView width is not fixed, column's width always changing depending on results. GridView的宽度不是固定的,列的宽度总是根据结果而变化。 How to make that TextBox width on top of columns auto changed when GridView's columns width changing? 当GridView的列宽更改时,如何使列顶部的TextBox宽度自动更改?

As you see in image, there are 3 columns. 如您在图像中看到的,共有3列。 White - TextBoxes (for filtering data), yellow - GridView 白色-文本框(用于过滤数据),黄色-GridView

过滤

This is my current code: 这是我当前的代码:

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="InsertName" runat="server" width="130px" placeholder="Name Filter..."></asp:TextBox>
    <asp:TextBox ID="InsertEmail" runat="server" width="130px" placeholder="Domain Filter..."></asp:TextBox>
    <asp:TextBox ID="InsertCountry" runat="server" width="130px" placeholder="Domain Filter..."></asp:TextBox>
    <br />
    <br />
</div>
    <asp:GridView ID="GridView1" runat="server" BackColor="#FFFFB3" width="390px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    </asp:GridView>
</form>

Here I set fixed widths for each TextBox and for GridView. 在这里,我为每个TextBox和GridView设置了固定宽度。 But here is problem, GridView have fixed width as I wrote width="390px" , but columns width always changing. 但是这里有问题,当我写width="390px" ,GridView具有固定的宽度,但是列的宽度总是在变化。 Have you ideas? 你有想法吗?

You cannot accomplish this in the current HTML markup since the DIV containing the text boxes is unaware of the GridView resizing. 您不能在当前的HTML标记中完成此操作,因为包含文本框的DIV无法识别GridView的大小。

Instead, you could use a HeaderTemplate inside a TemplateField like this (with a 100% text box width): 相反,您可以在这样的TemplateField内部使用HeaderTemplate (具有100%的文本框宽度):

<asp:GridView ID="GridView1" runat="server" BackColor="#FFFFB3" width="390px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
  <Columns>
    ...
    <asp:TemplateField HeaderText="Name">
      <HeaderTemplate>
        <asp:TextBox ID="InsertName" runat="server" width="100%" placeholder="Name Filter..."></asp:TextBox>
      </HeaderTemplate>
      <ItemTemplate>
      ...
      </ItemTemplate>
    </asp:TemplateField>
    ...
  </Columns>
</asp:GridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM