在一個ASP.Net web窗體中有一個GridView。我們希望在這個GridView中有一個固定大小的列,並且其中的文本也可以進行文字換行。我們無法實現它的工作。製作一個ASP.Net GridView列一個固定的大小並做文字換行
這裏是我試過的標記:
<asp:BoundField DataField="AssignmentDetails" HeaderText="Assignment"
SortExpression="AssignmentDetails" ItemStyle-Width="20" ItemStyle-Wrap="true">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
我們認爲這將使立柱偏出20個字符,使自動換行,但它沒有做到這一點。
*更新*
它現在的工作基礎上達倫的編碼樣本。這裏是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
包裝的實際字符將是最適合RowDataBound事件 - 基本上每20個字符添加一個換行符。 – Darren 2013-04-11 18:50:40
感謝您的回覆。你能展示編碼樣本嗎? – 2013-04-11 18:55:13
當然,一個來吧 – Darren 2013-04-11 19:17:14