zoukankan      html  css  js  c++  java
  • GridView小记

    1.绑定键值及读取

    前端设置:DataKeyNames=“Field1,Field2”;

    后台设置:GridView1.DataKeyNames=Gridview1.DataKeyNames = new string[] { "PositionID", "DepartmentID" };

    后台读取:string key=this.GridView1.DataKeys[i][0].ToString();

    2.可编辑列

    <asp:TemplateField HeaderText="Field1" ControlStyle-Width="100px" />
        <ItemTemplate>
            <asp:TextBox ID="Field1" runat="server" Text='<%# Bind("Field1") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>

    3.斑马线及鼠标停留变色

    前端:onrowdatabound="GridView1_RowDataBound"

    后台:

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++) //执行循环,保证每条数据都可以更新
            {
                if (e.Row.RowType == DataControlRowType.DataRow)  //首先判断是否是数据行
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#999'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
                if (i % 2 == 0)
                    this.GridView1.Rows[i].BackColor = System.Drawing.Color.Honeydew;
            }
        }

    4.导出EXCEL

    protected void btnExport_Click(object sender, EventArgs e)    

    {        

        Response.ContentEncoding = System.Text.Encoding.UTF8;//编码不能错,错了就会乱码,EXCEL解释不了格式就把HTML文本全部显示        

        Response.AppendHeader("Content-Disposition", "attachment;filename=SampleIssue.xls");        

        Response.ContentType = "application/ms-excel";        

        this.EnableViewState = false;        

        StringWriter sw= new StringWriter();        

        HtmlTextWriter htw = new HtmlTextWriter(sw);        

        GridView1.RenderControl(htw);        

        Response.Write(sw.ToString());        

        Response.End();    

    }

    /// <summary>    

    /// 导出EXCEL,要写一个空的VerifyRenderingInServerForm方法    

    /// </summary>    

    /// <param name="control"></param>    

    public override void VerifyRenderingInServerForm(Control control)    

    {        

    // Confirms that an HtmlForm control is rendered for    

    }

    如出现错误提示“RegisterForEventValidation can only be called during Render();”,则在<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>里面加入 EnableEventValidation="false"即可。

  • 相关阅读:
    python3 crypto winrandom import error
    Flask-Babel 中文支持(zh-CN和zh-Hans-CN)
    pip 安装psycopg的错误
    Aapache status / apache2ctl status 总是403
    为什么你还在用嵌入式的方式来使用mod_wsgi?
    Git中当add错误的时候怎么办?
    Python 内置彩蛋
    本人AI知识体系导航
    本人SW知识体系导航
    SSH密钥对登录的原理和实践
  • 原文地址:https://www.cnblogs.com/Hawk-Hong/p/9985014.html
Copyright © 2011-2022 走看看