如何在Gridview的Boundfield中包装文本?

7
我有一个表格,其中有几个按钮和一个网格视图。我正在尝试在Gridview的一个boundfield中包装文本。
我已经尝试在Gridview属性中设置RowStyle Wrap="true",并在boundfield属性中设置ItemStyle Wrap="true"和宽度Width,但没有起作用。
以下是我的aspx代码:
<table align="center" border="0" cellpadding="0" cellspacing="2" >
    <tr>
        <td></td>
        <td align="right">
            <asp:Button ID="btnAdd" runat="server" Text="Add Subscription" 
                onclick="btnAdd_Click" CausesValidation="False" />
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <p align="center" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px" >
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            </p>
        </td>
    </tr>
    <tr>
        <td align="left" colspan="2">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="SubscriptionID,UserID" 
                DataSourceID="SqlDSEmailSubscriptions" Width="90%" CellPadding="4" 
                EnableViewState="False" AllowPaging="True">
                <Columns>
                    <asp:TemplateField HeaderText="SubscriptionName" SortExpression="SubscriptionName">
                    <ItemTemplate>
                        <asp:LinkButton ID="lbtnSubscription" runat="server" CausesValidation="false" 
                        Text='<%# Eval("SubscriptionName")%>' OnClick="lbtnSubscription_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                    </asp:TemplateField> 

                    <asp:BoundField DataField="SubscriptionName" HeaderText="SubscriptionName" 
                        SortExpression="SubscriptionName" Visible="false" />

                    <asp:BoundField DataField="SubscriptionID" HeaderText="SubscriptionID" 
                        ReadOnly="True" SortExpression="SubscriptionID" />
                    <asp:BoundField DataField="ProductList" HeaderText="ProductList" 
                        SortExpression="ProductList" />
                    <asp:BoundField DataField="DivisionList" HeaderText="DivisionList" 
                        SortExpression="DivisionList" />
                    <asp:BoundField DataField="DisciplineList" HeaderText="DisciplineList" 
                        SortExpression="DisciplineList" />
                    <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" 
                        SortExpression="UserID" Visible="false" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDSEmailSubscriptions" runat="server" 
                ConnectionString="<%$ ConnectionStrings:SPRConnectionString %>" 

                SelectCommand="SELECT [SubscriptionID], [SubscriptionName], [ProductList], [DivisionList], [DisciplineList], [UserID] FROM [sprEmailSubscriptions] WHERE ([UserID] = @UserID) ORDER BY [SubscriptionName]">
                <SelectParameters>
                    <asp:SessionParameter Name="UserID" SessionField="userID" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
        </td>
    </tr>
</table>

建议为需要文本换行的列指定宽度。类似的问题已经存在:https://dev59.com/dVHTa4cB1Zd3GeqPRW6O 您也可以尝试以下内容:Gridview text not wrapping in IE8 希望能帮到您! - Vaibhav
你能否更新上面的标记,以准确地描述你想要包装的列以及你应用的样式?谢谢。 - Vaibhav
3个回答

12

将文本包装在具有固定长度的网格视图列中。

首先,在网格视图中创建要包装文本的列,使用 ItemTemplate

可以通过以下方式完成:

  • 选择网格视图 - 智能标记 > 编辑列
  • 从标题为Selectedfields的底部左侧框中选择列
  • 单击“将此字段转换为TemplateField”> 确定

在源代码中,您将看到以下代码:

<asp:TemplateField HeaderText="name" SortExpression="name">
    <EditItemTemplate>
       <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name")%>'></asp:TextBox>
    </EditItemTemplate>

    <ItemTemplate>
       <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

将列的宽度限制为像素:

<ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>' Width="200px"></asp:Label>
</ItemTemplate>

现在需要添加样式:

如果要将文本包装到列中,以应用于所有列或整个网格视图,则在page_load()事件中编写以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");    
}
如果要将文本在网格视图中的列中进行换行,只需在GridView1_RowDataBound()事件中编写以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;");
    }
}

检查GridView的单元格号码。

工作完成!


4

在项目模板中使用Div很好用

 <ItemTemplate>
                            <div style="word-wrap: break-word; width: 530px;>
                                <asp:Label ID="lblTermName" runat="server" Text='<%# Eval("TermName") %>' />
                            </div>
                        </ItemTemplate>

1

是的,您可以按每个“x”字符解析它,但将其放入列标题中以固定大小并定义换行“true”可能是更好的解决方案。

通过该选项,您将在每次获得相同的网格视图大小,并获得更好和更清晰的用户界面。

使用代码:

<asp:BoundField DataField:="Your_data_field" HeaderText="Your_text" sordExpression="Your_sort_expression" ItemStyle-wrap="true" ItemStyle-with="50" />

-wrap 可以工作,但 -with 无法工作。 - Shriram Panchal
尝试使用ItemStyle-Width="50"。 - Dick Bos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接