如何将文本包装在Gridview的boundfield中

GLP*_*GLP 6 asp.net text gridview boundfield word-wrap

我有一张桌子,桌子上有几个按钮和一个Gridview.我试图将文本包装在其中一个boundfieldGridview中.我尝试RowStyle Wrap="true"在Gridview属性中设置ItemStyle Wrap="true"Width在boundfield属性中设置和.但没有奏效.有人可以帮帮我吗?

以下是我的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>
Run Code Online (Sandbox Code Playgroud)

小智 9

在具有固定长度的gridview列中包装文本.

首先在gridview中创建列,其中文本将被包装为ItemTemplate.

这可以通过以下方式完成:

  • 选择gridview-smart tag> edit列
  • 选择标题为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>
Run Code Online (Sandbox Code Playgroud)

为列提供宽度限制,以像素为单位:

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

现在要添加样式:

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

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");    
}
Run Code Online (Sandbox Code Playgroud)

如果将列中的文本包装仅应用于网格视图的特定列,则在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;");
    }
}
Run Code Online (Sandbox Code Playgroud)

检查gridview的单元格编号.

完成工作!


小智 1

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

通过该选项,您每次都会获得相同的 gridview 大小以及更好、更清晰的用户界面。

使用代码:

<asp:BoundField DataField:="Your_data_field" HeaderText="Your_text" sordExpression="Your_sort_expression" ItemStyle-wrap="true" ItemStyle-with="50" />
Run Code Online (Sandbox Code Playgroud)