Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday 8 July 2015

Introduction:  some time we need to open the particular record from gridview in new webpage or we want to use the information of that particular row in next page. This can be done by writing some code in code behind file in ASP.NET.
In this article I will explain how to display gridview selected row data in new page.
Description:


Implementation:
I have created a table “Student Profile” and display the data from this table in new webpage. Suppose we want to show student profile in new page. I placed linkbutton when user visit the page and click on read more button user will be redirected to new page.

Design section of 1st webpage with gridview:
<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 HeaderText="Student Name">
        <ItemTemplate>
        <asp:Label ID="lblname" runat="server" Text='<%# Eval("Student_name") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Student_Image") %>' Width="250"/>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Class">
        <ItemTemplate>
       <asp:Label id="lblClass" runat="server" Text='<%# Eval("Class") %>'></asp:Label>
             <asp:HyperLink ID="hlRead" runat="server" Font-Bold="True" Font-Size="Small"
Font-Underline="True" ForeColor="#0099FF"
NavigateUrl='<%# Eval("Studentid","detail.aspx?id={0}") %>'>Read More</asp:HyperLink>
        </ItemTemplate>
        </asp:TemplateField> 
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
ASP.NET Code for 1st page:
In this page we will bind the gridview.
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 gridopen : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    public void BindGrid()
    {
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from student_profile", con);        
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
        }
    }
}

Design of Second Webpage: This is the page where we will redirect the information from 1st webpage.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
     <td colspan="2"><asp:Image ID="Image1" runat="server" Width="200px" Height="200px" /></td>
    </tr>
    <tr>
    <td><b>Student Name</b> :-</td>
     <td><asp:Label ID="lblname" runat="server"></asp:Label></td>
    </tr>
    <tr>
    <td><b>Class </b>:-</td>
     <td><asp:Label ID="lbldescription" runat="server"></asp:Label></td>
    </tr>
    </table> 
    </div>
    </form>
</body>
</html>

ASP.NET code using c#:
 In this page we get the data from database according to studentid and bind it to different labels and Image control.
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.Configuration;
using System.Data;

public partial class detail : System.Web.UI.Page
{
  
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Convert.ToInt32(Request.QueryString["id"]);
        BindData();
    }
    public void BindData()
    {
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from student_profile where studentid="+id+"", con);
           
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                lbldescription.Text = dt.Rows[0]["class"].ToString();
                lblname.Text = dt.Rows[0]["student_name"].ToString();
                Image1.ImageUrl = dt.Rows[0]["student_image"].ToString();
            }
        }
        catch (Exception ex)
        {
        }
    }

}
Categories: ,

0 comments:

Post a Comment