zoukankan      html  css  js  c++  java
  • 控制GridView中字段的长度,规范数据

    前台:

           <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <input id="cb_head" type="checkbox" name="cb_head" onclick="SelectAll(this,<%=GridView1.ClientID %>);" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="ChkChild1" runat="server" />
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                        <FooterStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    后台:

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //在RowDataBound事件中调下面的方法
            int[] cellIndexs = new int[4];
    
            this.setGridViewInfo(e,1, cellIndexs, 11);
        }
    
        #region
        /// <summary>
        /// 控制GridView中显示字符串的长度 并 在ToolTip中显示全部文本
        /// </summary>
        /// <param name="e">GridViewRowEventArgs e</param>
        /// <param name="cellIndexs">GridView中显示单元格的个数</param>
        /// <param name="maxStrLength">要显示的最大字符串长度</param>
        private void setGridViewInfo(System.Web.UI.WebControls.GridViewRowEventArgs e,int stata_index, int[] cellIndexs, int maxStrLength)
        {
            string CellText = string.Empty;
    
            //因为我的GridView前一列是CheckBox,所以i从1开始
            for (int i = stata_index; i < cellIndexs.Length; i++)
            {
                CellText = e.Row.Cells[i].Text.Trim();
    
                if (CellText.Length > maxStrLength)
                {
                    e.Row.Cells[i].Text = CellText.Substring(0, maxStrLength - 1) + " <strong style='Color:blue' mce_style='Color:blue'> ... </strong>";
    
                    e.Row.Cells[i].ToolTip = CellText;
                }
            }
        }
        #endregion
  • 相关阅读:
    JAVA基础知识-java文化基础和运行环境
    一个获得jdbc方式Connection的静态方法
    Hibernate学习笔记(二)
    JVM学习笔记
    chrome常用插件
    面向站长和网站管理员的Web缓存加速指南
    OFBiz进阶之环境搭建(eclipse)
    OFBiz之SVN下载地址
    OFBiz进阶之HelloWorld(一)创建热部署模块
    Sublime Text 3插件之SublimeTmpl:新建文件的模版插件
  • 原文地址:https://www.cnblogs.com/houlin/p/3790946.html
Copyright © 2011-2022 走看看