Categories
Asp, Asp.net

How to Redirect to Another Page by Click on LinkButton in GridView

How to Redirect to Another Page by Click on LinkButton in GridView

In this tutorial i will let you know that how can you pass value from GridView Row data value to another page.Here i used Linkbutton for this inside GridView, When you get redirected to the new page you can get the id in the query string for further data procesing.

Design View First Page:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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" >
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton runat="server" CommandArgument='<%# Eval("Id") %>' OnCommand="LinkButton_Click" Text="View Details"> </asp:LinkButton>  
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code View First page:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
        SqlConnection conn = new SqlConnection(con);
        conn.Open();
        string str = "select id from flatfile_logged";
        SqlCommand cmd = new SqlCommand(str, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void LinkButton_Click(Object sender, CommandEventArgs e) 
    {
        if (e.CommandArgument != null)
        {
            Response.Redirect("YourPage.aspx?IdPassed=" + e.CommandArgument.ToString());
        }
    } 
}

Design View Second Page:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yourpage.aspx.cs" Inherits="yourpage" %>

<!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">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code View Second page:-

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;

public partial class yourpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int passedId = Convert.ToInt32(Request.QueryString["IdPassed"]);
        string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
        SqlConnection conn = new SqlConnection(con);
        conn.Open();
        string str = "select * from flatfile_logged where id="+passedId+"";
        SqlCommand cmd = new SqlCommand(str, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}