惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
H
Help Net Security
腾讯CDC
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
D
Docker
V
V2EX
Last Week in AI
Last Week in AI
G
Google Developers Blog
IT之家
IT之家
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东

博客园 - Hawk_Yuan

JQGrid自動翻頁 Aspose.Words aspose.cell C#通过SQL读写文件 Aspose.Cells asp.net mvc 4 json大数据异常 提示JSON字符长度超出限制的异常[转载] 日期转换成字符串 LinkServer SoapHeader Credential 等等等 决心要创业成功的10个必备信念 成功人的共性 会计科目 [转]BOM分层六项原则 [转]ERP核心理念讲座系列(四) [转]ERP核心理念讲座系列(三) [转]ERP核心理念讲座系列(二) [转]ERP核心理念讲座系列(一)
GridView小记
Hawk_Yuan · 2018-11-19 · via 博客园 - Hawk_Yuan

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"即可。