在 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