【问题标题】:Display text in multiline inside a grid view in asp.net在 asp.net 的网格视图中以多行显示文本
【发布时间】:2010-10-20 04:49:18
【问题描述】:

我在我的 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>

【问题讨论】:

    标签: asp.net gridview multiline


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如何在 gridview 中放置一个 div 。有可能吗?
      • 我试过这个。但不为我工作。原始 asp 代码已添加到我的编辑问题中
      • 你的 gridview 代码全乱了。您的 EditTemplate 和 ItemTemplate 区域已合并
      【解决方案3】:

      您可以像这样将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>
      

      截图:

      【讨论】:

      • 我也试过这个。但不为我工作。原始的 asp 代码被添加到我的编辑问题中。 :(。我看到了屏幕截图。这正是我想要的
      • 这对我有用,中间有空格。如果用户输入一个 100 个字符的单词会怎样。然后它不起作用。请提出建议。
      【解决方案4】:

      通过应用 css 类尝试这样的操作

       .paraGraphtext
          {
              white-space: pre-wrap;   
          }
      
      
      
      
      
      <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label>
           </ItemTemplate>
      

      【讨论】:

      • 嘿,这不工作:(
      猜你喜欢
      • 2016-04-23
      • 2014-02-07
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2014-08-06
      • 1970-01-01
      相关资源
      最近更新 更多