在 asp.net 中的网格视图内以多行显示文本

发布于 2024-09-27 21:22:19 字数 3009 浏览 0 评论 0原文

我在我的 asp.net 应用程序中使用网格视图。在一列中,我需要显示描述(最少 5 个字符。最多 255 个字符)。我正在使用标签在该网格视图中保存描述。

但我的问题是,如果描述较大,它会在浏览器中延伸并显示在一行中。我想以多行(如段落)显示描述

我希望有人帮助我。整个网格视图代码如下所示

  <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>

I am using a grid view in my asp.net application. In one column i need to display description(minimum 5 characters. Maximum 255 characters).i am using a label to hold description in that grid view.

But my problem is that if the description is larger it stretches in the browser and show it in one line. I want to display description in multi line (like a paragraph)

I hope some one help me . the entire grid view code is shown below

  <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> <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这样的小城市 2024-10-04 21:22:19

试试这个...工作正常...在 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");
        }
    }
}

try this... working properly...for wrapping text in 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");
        }
    }
}
抚笙 2024-10-04 21:22:19

有时 ItemStyle Wrap="true" 不起作用。为了确保文本自动换行,请将 Label 围在 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>

Sometimes ItemStyle Wrap="true" doesn't work. To be certain your text wraps, surround the Label in a and set a width on the surrounding div.

EDIT

<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>
温柔嚣张 2024-10-04 21:22:19

您可以设置 ItemStyle TemplateField 到 < code>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

You can set the ItemStyle of the TemplateField to true like this:

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

Complete gridview code:

<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>

Screenshot:

alt text

旧夏天 2024-10-04 21:22:19

通过应用 css 类尝试这样

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





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

Try like this by applying the css class

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





<ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label>
     </ItemTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文