zoukankan      html  css  js  c++  java
  • 使用超链接跳转页面(GridView)

    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);
            }
        }
    }
    View Code

    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>
    View Code
     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);
                }
    View Code
  • 相关阅读:
    2019年9月25日星期三(STM32 ucos3)
    2019年9月23日星期一(STM32 spi接口 rc522)
    2019年9月21日星期六(STM32 spi接口flash)
    2019年9月20日星期五(STM32 flash)
    2019年9月19日星期四(STM32 rtc实时时钟 i2c协议)
    2019年9月18日星期三(STM32 adc转换)
    2019年9月17日星期二(STM32 串口 看门狗)
    2019年9月16日星期一(STM32 串口 超声波 单总线通讯)
    2019年9月12日星期四(STM32 pwm 串口 )
    Win7/8/8.1系统中针对SSD需要禁用和改动的服务(转)
  • 原文地址:https://www.cnblogs.com/songxia/p/4270824.html
Copyright © 2011-2022 走看看