Home > ASP.Net > Redirect to Another Page by Click on LinkButton in GridView

Redirect to Another Page by Click on LinkButton in GridView


Hi all,

try this example to Redirect to Another Page by Click on LinkButton in GridView

1) Open VS 2008 and create a new web site

2) Add New Web Page called “Default.aspx”  and from Toolbox drag and drop Gridview to be like this


<%@ 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>

In code behind :


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("Details.aspx?Id=" + e.CommandArgument.ToString());
 }
 }
}

3) Add another web page to display Details called “Details.aspx”


<%@ 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>

In code behind :


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["Id"]);
 string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
 SqlConnection conn = new SqlConnection(con);
 conn.Open();
 string str = "select * from flatfile_logged where id="+Id+"";
 SqlCommand cmd = new SqlCommand(str, conn);
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataTable dt = new DataTable();
 da.Fill(dt);
 GridView1.DataSource = dt;
 GridView1.DataBind();
 }
}

Hope this helps

Good Luck.

 

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: