<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="MBA.Web.test" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound" Width="379px" DataKeyNames="NewsID" onselectedindexchanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="NewsTitle" HeaderText="新闻标题" /> <asp:BoundField DataField="NewsCreateTiem" HeaderText="创建时间" /> <asp:TemplateField ShowHeader="False" > <ItemTemplate> <asp:Button ID="btxian" runat="server" CausesValidation="false" CommandName="select" Width="0px" Text="" Visible="True" /> </ItemTemplate> <ItemStyle Width="0px" /> </asp:TemplateField> </Columns> </asp:GridView> <asp:GridView ID="GridView2" runat="server" Width="379px"> </asp:GridView> <br /> </div> <br /> <br /> </form> </body> </html>
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MBA.Web { public partial class test : System.Web.UI.Page { BLL.News bll = new MBA.BLL.News(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { bind(); } } private void bind() { GridView1.DataSource = bll.GetList(" NewsID<30 "); GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //当鼠标停留时更改背景色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#EBFBBA'"); //当鼠标移开时还原背景色 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); Button btn = e.Row.FindControl("btxian") as Button; if (btn != null) { e.Row.Attributes.Add("onclick", "document.getElementById('" + btn.ClientID + "').click();"); //e.Row.Attributes.Add("style", "display:none"); e.Row.Cells[2].Style.Value = "display:none"; GridView1.HeaderRow.Cells[2].Style.Value = "display:none"; } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string str=this.GridView1.SelectedValue.ToString(); GridView2.DataSource = bll.GetList(" NewsID=" + Convert.ToInt32(str)); GridView2.DataBind(); } } }