2010-10-20 77 views
1

我在我的asp.net應用程序中使用網格視圖。在一列中,我需要顯示描述(最少5個字符,最多255個字符)。我使用一個標籤來保存網格視圖中的描述。在asp.net中顯示網格視圖內的多行文本

但我的問題是,如果描述較大,它會在瀏覽器中展開並顯示在一行中。我想在多行顯示說明(如段落)

我希望有人幫助我。整個網格視圖代碼如下所示

<asp:GridView ID="gv_View_Documents" runat="server" AllowSorting="true" DataKeyNames="DocumentName,Description" SkinID="customGridview" AutoGenerateColumns="false" OnSorting="gv_View_Documents_Sorting" OnRowCancelingEdit="gv_View_Documents_RowCancelingEdit" OnRowCommand="gv_View_Documents_RowCommand" 
        OnRowEditing="gv_View_Documents_RowEditing" OnRowUpdating="gv_View_Documents_RowUpdating" > 
        <Columns> 
         <asp:TemplateField HeaderText="Document Name" HeaderStyle-Width="200" HeaderStyle-CssClass="GridHeaderStyle" SortExpression="DocumentName" > 
          <ItemTemplate> 
           <asp:LinkButton CommandName="ViewDocument" CssClass="GridHeaderStyle" ID="hlnk_View_Document" runat="server" CommandArgument='<%# Bind("DocumentName") %>' Text='<%# Bind("DocumentName") %>'> 
           </asp:LinkButton> 
          </ItemTemplate> 
         </asp:TemplateField> 


         <asp:TemplateField HeaderStyle-Width="200" HeaderText="Description"> 

         <ItemTemplate> 


          <asp:Label ID="lbl_gv_DocumentDescription" runat="server" Text='<%# Bind("Description") %>' ></asp:Label></ItemTemplate> 

          <EditItemTemplate> 
          <asp:TextBox ID="txt_gv_EditDescription" MaxLength="250" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox> 
          </EditItemTemplate> 
          </asp:TemplateField> 

          <asp:TemplateField HeaderStyle-Width="50" HeaderStyle-CssClass="GridHeaderStyle" ShowHeader="False" > 
          <EditItemTemplate> 
          <asp:LinkButton ID="Bttn_Update_Description" ForeColor=" #555555" runat="server" CausesValidation="False" 
           CommandName="Update" Text="Update"></asp:LinkButton>&nbsp;<asp:LinkButton ID="Bttn_Cancel_Settings" ForeColor=" #555555" runat="server" CausesValidation="False" 
           CommandName="Cancel" Text="Cancel"></asp:LinkButton></EditItemTemplate><ItemTemplate> 
          <asp:LinkButton ID="Bttn_Edit_Description" ForeColor=" #555555" runat="server" CausesValidation="False" CommandName="Edit" 
           Text="Edit" ></asp:LinkButton></ItemTemplate><ControlStyle CssClass="edit" /> 
        </asp:TemplateField> 


        </Columns> 
        </asp:GridView> 

回答

1

可以將TemplateFieldItemStyle設置爲true這樣的:

<ItemStyle Wrap="true" Width="100px" /> 

完整gridview的代碼:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemStyle Wrap="true" Width="100px" /> 
      <ItemTemplate> 
       <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Label ID="Label3" runat="server" Text='<%# Eval("Age") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

截圖:

alt text

+1

文本換行它不工作:( – 2010-10-20 06:15:25

+0

奇怪,它適用於我(見編輯)。 – bla 2010-10-20 06:47:10

+0

我也試過。但不爲我工作。在我編輯的問題中添加了原始的asp代碼。 :(。和我看到了屏幕截圖。這正是我想要的 – 2010-10-20 08:42:29

0

有時ItemStyle Wrap =「true」不起作用。爲了確定你的文字包裝,將標籤包圍在a中並在周圍的div上設置一個寬度。

編輯

<Columns> 
    <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>"></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField> 
     <ItemTemplate> 
      <div style="width:100px;"> 
       <asp:Label ID="Label2" runat="server" Text="<%# Eval("Name") %>"></asp:Label> 
      </div> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Label ID="Label3" runat="server" Text="<%# Eval("Age") %>"></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 
+0

怎麼能我在gridview裏面放了一個div,可能嗎? – 2010-10-20 06:15:51

+0

看看我編輯的答案 – TheGeekYouNeed 2010-10-20 06:49:28

+0

我試過了這個,但是對我沒有效果,原來的asp代碼添加在我編輯的問題中 – 2010-10-20 08:42:07

1

通過應用CSS類

.paraGraphtext 
    { 
     white-space: pre-wrap; 
    } 





<ItemTemplate> 
      <asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label> 
    </ItemTemplate> 
+0

嘿,這是不工作:( – KiranSolkar 2014-12-31 07:52:26

2

嘗試這樣試試這個... ...正常工作中的GridView

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;width:100px"); 
     } 
    } 
}