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

推荐订阅源

V
V2EX
爱范儿
爱范儿
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
B
Blog RSS Feed
博客园 - 聂微东
G
GRAHAM CLULEY
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Scott Helme
Scott Helme
AI
AI
S
Security Affairs
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
AWS News Blog
AWS News Blog
T
Threatpost
Cyberwarzone
Cyberwarzone
www.infosecurity-magazine.com
www.infosecurity-magazine.com
U
Unit 42
V
Vulnerabilities – Threatpost
J
Java Code Geeks
博客园 - Franky
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
NISL@THU
NISL@THU
D
Docker
小众软件
小众软件
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
A
Arctic Wolf
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
Forbes - Security
Forbes - Security
量子位
PCI Perspectives
PCI Perspectives
美团技术团队
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
有赞技术团队
有赞技术团队
腾讯CDC
P
Proofpoint News Feed
S
Security @ Cisco Blogs
G
Google Developers Blog
C
Cisco Blogs

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