1. the html markup
<div> <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Id", "~/ThreadsTest/GridView/DetailsInformation.aspx?Id={0}") %>' Text='<%#Eval("Id") %>'>></asp:HyperLink> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowSelectButton="true" SelectText="View Details" /> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" /> <asp:TemplateField> <ItemTemplate> <asp:HyperLink runat="server" NavigateUrl='<%# string.Format("~/ThreadsTest/GridView/DetailsInformation.aspx?Id={0}&Name={1}&City={2}", HttpUtility.UrlEncode(Eval("Id").ToString()), HttpUtility.UrlEncode(Eval("Name").ToString()), HttpUtility.UrlEncode(Eval("City").ToString())) %>' Text="View Details" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="btnExportToEXCEL" runat="server" Text="Export to Excel" OnClick="btnExportToEXCEL_Click"/> <br /> <hr /> <asp:GridView ID="GridView2" runat="server" AllowPaging="true" OnRowCommand="GridView2_RowCommand" PageSize="5" OnPageIndexChanging="GridView2_PageIndexChanging" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="150" /> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" /> <asp:CommandField ShowSelectButton="true" SelectText="View Details" /> </Columns> </asp:GridView> </div>
2.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.IO; using System.Drawing; namespace _201502ThreadsTest.ThreadsTest { public partial class PassHyperLinkInGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //BindGridview(GridView1); //BindGridview(GridView2); } } //Bind Gridview public void BindGridview(GridView grv) { string sql = "select * from Customer"; using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString)) { SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); sda.Fill(dt); grv.DataSource = dt; grv.DataBind(); } } //Paging protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; this.BindGridview(GridView1); } //Export to Excel protected void btnExportToEXCEL_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); //To Export all pages GridView1.AllowPaging = false; this.BindGridview(GridView1); GridView1.HeaderRow.BackColor = Color.White; foreach (TableCell cell in GridView1.HeaderRow.Cells) { cell.BackColor = GridView1.HeaderStyle.BackColor; } foreach (GridViewRow row in GridView1.Rows) { row.BackColor = Color.White; foreach (TableCell cell in row.Cells) { if (row.RowIndex % 2 == 0) { cell.BackColor = GridView1.AlternatingRowStyle.BackColor; } else { cell.BackColor = GridView1.RowStyle.BackColor; } cell.CssClass = "textmode"; } } GridView1.RenderControl(hw); //style to format numbers to string string style = @"<style> .textmode { } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); int Id = Convert.ToInt32(GridView2.Rows[index].Cells[0].Text); Session["Id"] = Id; Response.Redirect("~/ThreadsTest/GridView/DetailsInformation.aspx"); } protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView2.PageIndex = e.NewPageIndex; this.BindGridview(GridView2); } } }
3. 目标页
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table border="1"> <tr> <td> <b>Id</b> </td> <td> <asp:Label ID="lblId" runat="server"></asp:Label> </td> </tr> <tr> <td> <b>Name</b> </td> <td> <asp:Label ID="lblName" runat="server"></asp:Label> </td> </tr> <tr> <td> <b>City</b> </td> <td> <asp:Label ID="lblCity" runat="server"></asp:Label> </td> </tr> </table> <br /> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/ThreadsTest/GridView/PassHyperLinkInGridView.aspx">Back to Previous page </asp:HyperLink> </div> </form> </body> </html>
if (!IsPostBack) { //lblId.Text = Request.QueryString["Id"]; //lblName.Text = Request.QueryString["Name"]; //lblCity.Text = Request.QueryString["City"]; string id = Session["Id"].ToString(); Response.Write(id); }