1.首先拖一个GridView到窗体,并选择数据源,然后配属性Colums如下图:
2.对它事件写代码如下:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridView _gridView = (GridView)sender; int _selectedIndex = int.Parse(e.CommandArgument.ToString()); string _commandName = e.CommandName; if (_commandName == "DownLoad") { if (_gridView.SelectedIndex > -1) { _gridView.SelectedRow.ForeColor = System.Drawing.Color.Black; } _gridView.SelectedIndex = _selectedIndex; _gridView.SelectedRow.ForeColor = System.Drawing.Color.Red; }
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0]; string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, ""); e.Row.Attributes["Onclick"] = _jsSingle;
} }
3.重写 Render(HtmlTextWriter writer) protected override void Render(HtmlTextWriter writer) { // The client scripts for GridView1 were created in GridView1_RowDataBound foreach (GridViewRow r in GridView1.Rows) { if (r.RowType == DataControlRowType.DataRow) { Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00"); Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl01"); Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl02"); } }
base.Render(writer); }