2013-04-11 36 views
0

在一個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 
+0

包裝的實際字符將是最適合RowDataBound事件 - 基本上每20個字符添加一個換行符。 – Darren 2013-04-11 18:50:40

+0

感謝您的回覆。你能展示編碼樣本嗎? – 2013-04-11 18:55:13

+0

當然,一個來吧 – Darren 2013-04-11 19:17:14

回答

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 

我沒有測試此代碼 - 我剛纔敲起來,例如,但它會給你一個關於如何進行的想法。請不要複製/粘貼,然後發表評論,它不能立即開箱即用。需要了解更多信息才能提供確切的工作示例。 :)

+0

感謝Darren編碼示例,也感謝在VB.Net中顯示它,因爲我不知道C#。我會試試看,並讓你知道結果。 – 2013-04-12 12:38:36

+0

謝謝。它現在基於你的編碼工作。我更新了發佈。 – 2013-04-12 14:02:16

+0

不客氣:)在這個RowDataBinding中你可以做很多事情。你會發現自己越來越多地使用它。 – Darren 2013-04-12 14:05:04

0

ItemStyle-Width設置列的像素寬度,而不是字符數。如果內容不能以20像素打包,則會擴展該列。

+0

感謝您的快速回復。我嘗試了幾個數字,如100,150和700,但列寬度從不改變大小。 – 2013-04-11 18:54:12

+0

默認情況下是在必要時進行換行。可能有其他的CSS風格干擾你正在嘗試做的事情。你有沒有像Firebug這樣的工具檢查過html? – 2013-04-11 19:03:52

+0

感謝您的回覆。我爲我們的GridView和DetailsView使用了Darren的技術。 – 2013-04-12 13:52:24

1

ItemStyle-CssClass="WrappedText",並在CSS做:

.WrappedText 
{ 
    word-break: break-all; 
    word-wrap: break-word; 
} 
相關問題