1

我有一个显示产品信息(下面的代码)的网格视图,现在我想限制描述字段,以便在有长描述时只显示前几个,并调整图像大小以适合图像列(并使图片,描述栏比其他栏大一点

谢谢

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductId" OnRowDeleting="gvProducts_RowDeleting" OnSelectedIndexChanged="gvProducts_SelectedIndexChanged">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="Productname" HeaderText="Name" />
        <asp:BoundField DataField="ProductDescription" HeaderText="Description"  />
        <asp:BoundField DataField="ProductSupplier" HeaderText="Supplier" />
        <asp:BoundField DataField="ProductCategory" HeaderText="Category" />
        <asp:BoundField DataField="ProductSubCategory" HeaderText="SubCategory" />
        <asp:BoundField DataField="ProductVAT" HeaderText="VAT" />
        <asp:BoundField DataField="ProductStock" HeaderText="Stock" />
        <asp:ImageField DataImageUrlField="ProductImage" HeaderText="Image"/>
        <asp:CheckBoxField DataField="ProductActive" HeaderText="Active" />
        <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
    </Columns>
    <SelectedRowStyle CssClass="selectedrow" />
</asp:GridView>
4

5 回答 5

1

使用css我们可以限制要显示的描述文本。像这样....

<asp:GridView ID="gvProducts" runat="server" CssClass="GridCSS" AutoGenerateColumns="False" DataKeyNames="ProductId" OnRowDeleting="gvProducts_RowDeleting" OnSelectedIndexChanged="gvProducts_SelectedIndexChanged">
    <Columns>
-----------------------
-----------
---------
<asp:BoundField DataField="ProductDescription" HeaderText="Description" ItemStyle-CssClass="limittext" />
------------------
---------
-------------

</Columns>

</asp:GridView>

CSS 样式:

.GridCSS {
        table-layout:fixed; 
        width:100%; 
    }
    .GridCSS .limittext{
        overflow: hidden; 
        text-overflow: ellipsis; 
        white-space: nowrap;        
    }
于 2012-12-24T18:49:12.783 回答
0

您可以使用 fw 4.5 的掩码或一些外部插件 lika Jquery Mask。或者您可以将它们设为模板项并放置一些代码,例如 <%# Eval("ProductDescription").SubString(0,50) %>

于 2012-12-24T15:24:48.307 回答
0
<table style="table-layout: fixed">
                    <tr>
                        <td>
                            <telerik:RadGrid ID="radGVEmployee" runat="server" AllowSorting="True" GridLines="None"
                                AutoGenerateColumns="False" CellSpacing="0" OnNeedDataSource="radGVEmployee_NeedDataSource"
                                Visible="True" OnSortCommand="radGVEmployee_SortCommand" AllowPaging="true" OnPageIndexChanged="radGVEmployee_PageIndexChanged"
                                Width="900px" OnItemDataBound="radGVEmployee_ItemDataBound" OnItemCommand="radGVEmployee_ItemCommand"
                                PageSize="5">
                                <FilterMenu EnableTheming="True">
                                    <CollapseAnimation Duration="200" Type="OutQuint" />
                                </FilterMenu>
                                <GroupingSettings CaseSensitive="False" />
                                <MasterTableView DataKeyNames="UsrId" AllowFilteringByColumn="true" TableLayout="Fixed"
                                    Visible="true" AllowMultiColumnSorting="false">
                                    <NoRecordsTemplate>
                                        <div style="width: 700px;" align="center">
                                            There is No Data to display</div>
                                    </NoRecordsTemplate>
                                    <CommandItemSettings ExportToPdfText="Export to PDF" />
                                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                                    </RowIndicatorColumn>
                                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                                    </ExpandCollapseColumn>
                                    <Columns>
                                        <telerik:GridTemplateColumn SortExpression="UserName" DataField="UserName" HeaderText="UserName"
                                            HeaderStyle-Width="60px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <asp:Label ID="lblItmUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn SortExpression="FullName" DataField="FullName" HeaderText="FullName"
                                            HeaderStyle-Width="60px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <asp:Label ID="lblItmFullName" runat="server" Text='<%# Eval("FullName") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn SortExpression="MobileNo" DataField="MobileNo" HeaderText="MobileNo"
                                            HeaderStyle-Width="40px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <asp:Label ID="lblItmMobileNo" runat="server" Text='<%# Eval("MobileNo") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn SortExpression="CompanyName" DataField="CompanyName"
                                            HeaderText="CompanyName" HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <asp:Label ID="lblItmCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn SortExpression="BranchName" DataField="BranchName" HeaderText="BranchName"
                                            HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <asp:Label ID="lblItmBranchName" runat="server" Text='<%# Eval("BranchName") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn SortExpression="Designation" DataField="Designation"
                                            HeaderText="Designation" HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <asp:Label ID="lblItmDesignation" runat="server" Text='<%# Eval("Designation") %>'></asp:Label>
                                                <asp:Label ID="lblItmLockStatus" runat="server" Text='<%# Eval("LockStatus") %>'
                                                    Visible="false"></asp:Label>
                                                <asp:Label ID="lblItmUserStatus" runat="server" Text='<%# Eval("UserStatus") %>'
                                                    Visible="false"></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn SortExpression="UserImage" DataField="UserImage" HeaderText="UserImage"
                                            HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center"
                                            AllowFiltering="false">
                                            <ItemTemplate>
                                                <asp:Image ID="lblUserImage" runat="server" ImageUrl='<%#Eval("ImageName")!=DBNull.Value? 
                                                  System.Configuration.ConfigurationManager.AppSettings["AzureImagePath"]+Eval("ImageName"):System.Configuration.ConfigurationManager.AppSettings["UserImagePath"]+"profile-empty-head.gif"%>'
                                                    Height="100px" Width="90px" />
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn AllowFiltering="false" DataField="Actions" HeaderText="Actions"
                                            HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Center">
                                            <%--OnClientClick='radconfirm("Are you sure you want to Change this LockStatus?", ClientGridCallback,300,150); return false;' --%>
                                            <ItemTemplate>
                                                <asp:ImageButton runat="server" ID="imgLockStatus" ImageUrl="~/App_Themes/DefaultClient/images/unlock.png"
                                                    CommandName="ChangeLockStatus" CommandArgument='<%# Eval("UsrId") %>' ToolTip="UserLockStatus"
                                                    AlternateText="LockStatus" OnClientClick="return blockConfirm('Are you sure you want to Change Lock Status?', event, 340, 150,'','Change Lock Status')" />
                                                <asp:ImageButton runat="server" ID="imgUserStatus" ImageUrl="~/App_Themes/DefaultClient/images/select.png"
                                                    CommandName="ChangeUserStatus" CommandArgument='<%# Eval("UsrId") %>' ToolTip="UserActivationStatus"
                                                    AlternateText="UserActivationStatus" OnClientClick="return blockConfirm('Are you sure you want to Change Activation Status?', event, 340, 150,'','Change Activation Status')" />
                                                <asp:ImageButton runat="server" ID="imgResetPassword" ImageUrl="~/App_Themes/DefaultClient/images/logoff.png"
                                                    CommandName="ResetPassword" CommandArgument='<%# Eval("UsrId") %>' ToolTip="ResetPassword"
                                                    Width="22px" Height="22px" AlternateText="ResetPassword" OnClientClick="return blockConfirm('Are you sure you want to Reset Password?', event, 340, 150,'','Reset Password')"  />
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </telerik:GridTemplateColumn>
                                        <%--   <telerik:GridButtonColumn ConfirmText="Are you sure you want to Change LockStatus ?<br /><br /><br />"
                                            ConfirmDialogType="RadWindow" HeaderStyle-Width="15px" ConfirmDialogHeight="150px"
                                            HeaderText="LockStatus" HeaderStyle-HorizontalAlign="Center" CommandName="ChangeLockStatus"
                                            ButtonType="ImageButton" ImageUrl="~/App_Themes/DefaultClient/images/unlock.png"
                                            ItemStyle-HorizontalAlign="Center" ConfirmTitle="Delete Currency" UniqueName="ChangeLockStatus">
                                        </telerik:GridButtonColumn>
                                        <telerik:GridButtonColumn ConfirmText="Are you sure you want to Change ActivationStatus ?<br /><br /><br />"
                                            ConfirmDialogType="RadWindow" HeaderStyle-Width="15px" ConfirmDialogHeight="150px"
                                            HeaderText="ActivationStatus" HeaderStyle-HorizontalAlign="Center" CommandName="ChangeUserStatus"
                                            ButtonType="ImageButton" ImageUrl="~/App_Themes/DefaultClient/images/unlock.png"
                                            ItemStyle-HorizontalAlign="Center" ConfirmTitle="Delete Currency" UniqueName="ChangeUserStatus">
                                        </telerik:GridButtonColumn>--%>
                                    </Columns>
                                    <EditFormSettings>
                                        <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                        </EditColumn>
                                    </EditFormSettings>
                                </MasterTableView>
                                <GroupingSettings CaseSensitive="False" />
                                <ClientSettings>
                                    <ClientEvents OnGridCreated="GridCreated" />
                                </ClientSettings>
                                <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                            </telerik:RadGrid>
                        </td>
                    </tr>
                </table>

尝试这个 ..........

于 2013-04-06T12:29:51.717 回答
0

如果您正在使用 MVC,则可以在将其绑定到 GW 之前更改控制器中的 GridView 数据。如果您不使用 MVC,我唯一能提供的是:在您的 RowDataBound 事件中,检查描述是否超过您定义的最大值。字符,然后截断它。

我知道这不是一个正确的答案,但它可能会给你一些想法。由于我正在工作,现在无法提供代码,但我稍后会检查您的帖子是否没有答案。

进一步阅读:

http://www.dotnetspider.com/resources/43687-How-show-formatted-html-content-grid-view.aspx http://forums.asp.net/t/1391922.aspx/1

于 2012-12-24T15:30:50.580 回答
0

尝试这个

<asp:TemplateField HeaderText="Description">
<ItemTemplate>

<%# Eval("ProductDescription").ToString().Length > 50 ? Eval("ProductDescription").ToString().Substring(0,50)+"..." : Eval("ProductDescription").ToString() %>

</ItemTemplate>
</asp:TemplateField>

如果文本超过 50 个字符,则显示前 50 个字符,否则显示整个文本。

另一种选择是从数据库或产品对象返回一个简短的描述

 public string ShortDescription
 {
    get 
    { 
        return ProductDescription.Length > 50 ? ProductDescription.Substring(0,50) : ProductDescription;
    }
 }
于 2012-12-24T16:24:24.583 回答