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

推荐订阅源

Recent Announcements
Recent Announcements
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
博客园 - 司徒正美
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
小众软件
小众软件
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
A
About on SuperTechFans
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
B
Blog RSS Feed
G
Google Developers Blog
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)

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