0

在 ASP.Net Web 表单中有一个 GridView。我们希望此 GridView 中的 1 列具有固定大小,并且其中的文本也进行自动换行。我们无法让它工作。

这是我尝试过的标记:

<asp:BoundField DataField="AssignmentDetails" HeaderText="Assignment" 
    SortExpression="AssignmentDetails" ItemStyle-Width="20" ItemStyle-Wrap="true">

    <HeaderStyle HorizontalAlign="Left" />
    <ItemStyle HorizontalAlign="Left" />
</asp:BoundField>

我们认为这会使列宽 20 个字符并使单词换行,但事实并非如此。

* 更新 *

它现在基于 Darren 的编码示例工作。这是 GridView 的完整标记和使用他的技术的代码隐藏:

        <asp:GridView
            ID="GridViewSummary" 
            runat="server" 
            AllowSorting="True" 
            AutoGenerateColumns="False" 
            DataKeyNames="ID" 
            Width="691px" 
            AllowPaging="True"
            PageSize="5"
            OnRowDataBound="GridViewSummary_RowDataBound">

            <Columns>
                <asp:BoundField DataField="AssignmentDate" HeaderText="Date" 
                    SortExpression="AssignmentDate" DataFormatString="{0:MM/dd/yyyy}">

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>

                <asp:BoundField DataField="AssignmentDueDate" HeaderText="Date Due" 
                    SortExpression="AssignmentDueDate" DataFormatString="{0:MM/dd/yyyy}">

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>

                <asp:BoundField DataField="Class" HeaderText="Class" 
                    SortExpression="Class">

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>

                <asp:BoundField DataField="TeacherName" HeaderText="Teacher" 
                    SortExpression="TeacherName">

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>

                <asp:TemplateField HeaderText="Assignment" SortExpression="AssignmentDetails">
                    <ItemTemplate>
                        <asp:Label ID="LabelAssignment" runat="server" Text='<%# Bind("AssignmentDetails") %>'></asp:Label>
                    </ItemTemplate>

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" Wrap="True" />
                </asp:TemplateField>

                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:Button 
                            ID="ButtonSelect" 
                            runat="server" 
                            CausesValidation="False" 
                            CommandName="Select" 
                            Text="Select Assignment Details" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

代码隐藏:

Protected Sub GridViewSummary_RowDataBound(sender As Object, e As GridViewRowEventArgs)

    ' Deal with Data type rows, and not headers etc.
    '-----------------------------------------------
    If (e.Row.RowType = DataControlRowType.DataRow) Then

        Dim lblAssignment As Label = e.Row.FindControl("LabelAssignment")

        ' Call a recursive method and insert a line break every 20 chars.
        '----------------------------------------------------------------
        lblAssignment.Text = InsertlineBreak(lblAssignment.Text)
    End If
End Sub

Function InsertlineBreak(ByVal original As String) As String

    Dim MaxStringLength As Int16 = 20

    If original.Length > MaxStringLength Then
        Dim indexOfSpace = original.IndexOf(" ", MaxStringLength - 1)
        If indexOfSpace <> -1 AndAlso indexOfSpace <> original.Length - 1 Then
            Dim firstString As String = original.Substring(0, indexOfSpace)
            Dim secondString As String = original.Substring(indexOfSpace)

            Return firstString & "<br/>" & InsertlineBreak(secondString)
        Else
            Return original
        End If
    Else
        Return original
    End If
End Function

我还将这项技术应用于我们的 DetailsView 以及在此标记和代码隐藏中显示:

                <asp:TemplateField HeaderText="Details:" SortExpression="AssignmentDetails">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBoxAssignmentDetails" runat="server" Text='<%# Bind("AssignmentDetails") %>' TextMode="MultiLine"
                            rows="5"></asp:TextBox>
                    </EditItemTemplate>

                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBoxAssignmentDetails" runat="server" Text='<%# Bind("AssignmentDetails") %>' TextMode="MultiLine"
                            rows="5"></asp:TextBox>
                    </InsertItemTemplate>

                    <ItemTemplate>
                        <asp:Label 
                            ID="LabelAssignmentDetails" 
                            runat="server" 
                            Text='<%# Bind("AssignmentDetails") %> '
                            OnDataBinding="LabelAssignmentDetails_DataBinding">
                        </asp:Label>
                    </ItemTemplate>

                    <ItemStyle ForeColor="Blue" />
                </asp:TemplateField>

代码隐藏:

Protected Sub LabelAssignmentDetails_DataBinding(sender As Object, e As EventArgs)

    Dim lblAssignment As Label = DetailsView.FindControl("LabelAssignmentDetails")

    ' Call a recursive method and insert a line break every 20 chars.
    '----------------------------------------------------------------
    lblAssignment.Text = InsertlineBreak(lblAssignment.Text)
End Sub
4

3 回答 3

1

好的,所以项目宽度不起作用,因为它是基于像素的,而不是 Jason 所说的基于字符的。

您需要为网格中的每一行处理 RowDataBound 事件。

这个例子是在 VB 中,虽然我敢肯定将它转换为 C# 对你来说不会太难。

在你的代码隐藏中做这样的事情 - 用你的网格名称替换“MyGridView”。

首先,将您的绑定字段更改为模板字段;更容易控制..在那个模板中放一个Literal可以保存你的文本,在这个例子中调用这个 MyLit

Private Sub MyGridView(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowDataBound
        '' only want to deal with Data type rows, and not headers etc
        If (e.Row.RowType = DataControlRowType.DataRow) Then

             '' get this instance of the object you're binding (assuming object is Assignment)
             Dim assignment As Assignment = CType(e.Row.DataItem, Assignment)
             '' now get the literal control where you will be putting your text
            Dim MyLit as Literal = row.FindControl("MyLit")

            '' you can now call a recursive method and insert a line break every 20 chars
            MyLit.Text = InsertlineBreak(assignment.TextToSplitUp)

        End If

End Sub


 Function InsertlineBreak(ByVal original As String) As String
        Dim MaxStringLength As Int16 = 20

        If original.Length > MaxStringLength Then
            Dim indexOfSpace = original.IndexOf(" ", MaxStringLength - 1)
            If indexOfSpace <> -1 AndAlso indexOfSpace <> original.Length - 1 Then
                Dim firstString As String = original.Substring(0, indexOfSpace)
                Dim secondString As String = original.Substring(indexOfSpace)

                Return firstString & Chr(10) & InsertlineBreak(secondString)
            Else
                Return original
            End If
        Else
            Return original
        End If
    End Function

我还没有测试过这段代码——例如,我刚刚敲过它,尽管它会给你一个关于如何继续的想法。请不要只是复制/粘贴然后评论它不能直接使用。需要知道更多才能给出一个确切的工作示例。:)

于 2013-04-11T19:27:16.333 回答
1

设置ItemStyle-CssClass="WrappedText",并在 CSS 中执行:

.WrappedText
{
  word-break: break-all;
  word-wrap: break-word;
}
于 2014-07-23T05:12:30.653 回答
0

ItemStyle-Width设置列的像素宽度,而不是字符数。如果内容不能以 20 像素包装,则会扩展列。

于 2013-04-11T18:49:18.857 回答