Send GridView Data To Another Page In ASP.NET Web Forms | Learn ASP.NET C#

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DATA_SENDING_GRIDVIEW_ANOTHER_PAGE.WebForm1" %>


<!DOCTYPE html>


<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" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

                <Columns>

                    <asp:CommandField HeaderText="SELECT" ShowSelectButton="True" />

                    <asp:TemplateField HeaderText="ID">

                        <ItemTemplate>

                            <asp:Label ID="LabelId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="NAME">

                        <ItemTemplate>

                            <asp:Label ID="LabelName" runat="server" Text='<%# Eval("name") %>'></asp:Label>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="AGE">

                        <ItemTemplate>

                            <asp:Label ID="LabelAge" runat="server" Text='<%# Eval("age") %>'></asp:Label>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="IMAGE">

                        <ItemTemplate>

                            <asp:Image ID="Image1" runat="server" Height="100" Width="100" ImageUrl='<%# Eval("image") %>'/>

                        </ItemTemplate>

                    </asp:TemplateField>

                </Columns>

                <FooterStyle BackColor="#CCCCCC" />

                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />

                <RowStyle BackColor="White" />

                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

                <SortedAscendingCellStyle BackColor="#F1F1F1" />

                <SortedAscendingHeaderStyle BackColor="#808080" />

                <SortedDescendingCellStyle BackColor="#CAC9C9" />

                <SortedDescendingHeaderStyle BackColor="#383838" />

            </asp:GridView>

        </div>

    </form>

</body>

</html>

Above File is DATA_SENDING_GRIDVIEW_ANOTHER_PAGE\WebForm1.aspx















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;

namespace DATA_SENDING_GRIDVIEW_ANOTHER_PAGE
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGridView();
            }
        }

        void bindGridView()
        {
            try
            {
                using(SqlConnection con = new SqlConnection(cs))
                {
                    string query = "SELECT * FROM tbl";
                    SqlDataAdapter sda = new SqlDataAdapter(query,con);
                    DataTable data = new DataTable();
                    sda.Fill(data);
                    GridView1.DataSource = data;
                    GridView1.DataBind();
                }
            }
            catch (SqlException ex)
            {
                Response.Write("SqlException: " + ex.Message);
            }
            catch (Exception ex)
            {
                Response.Write("Exception: " + ex.Message);
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            Label lblId = (Label)row.FindControl("LabelId");
            Label lblName = (Label)row.FindControl("LabelName");
            Label lblAge = (Label)row.FindControl("LabelAge");
            Image img = (Image)row.FindControl("Image1");

            //Response.Redirect("WebForm2.aspx?id=" +lblId.Text+"&name=" +lblName.Text+"&age="+lblAge.Text);

            Session["id"] = lblId.Text;
            Session["name"] = lblName.Text;
            Session["age"] = lblAge.Text;
            Session["img"] = img.ImageUrl.ToString();
            Response.Redirect("WebForm2.aspx");
        }
    }
}
Above File is DATA_SENDING_GRIDVIEW_ANOTHER_PAGE\WebForm1.aspx.cs

















<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="DATA_SENDING_GRIDVIEW_ANOTHER_PAGE.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width:400px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table cellpadding="4" cellspacing="4" class="auto-style1">
                <tr>
                    <td>ID</td>
                    <td>
                        <asp:TextBox ID="IdTextBox" runat="server" Width="173px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>NAME</td>
                    <td>
                        <asp:TextBox ID="NameTextBox" runat="server" Width="173px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>AGE</td>
                    <td>
                        <asp:TextBox ID="AgeTextBox" runat="server" Width="173px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>IMAGE</td>
                    <td>
                        <asp:Image ID="Image1" runat="server" Height="100" Width="100"/>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>
Above File is DATA_SENDING_GRIDVIEW_ANOTHER_PAGE\WebForm2.aspx

















using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DATA_SENDING_GRIDVIEW_ANOTHER_PAGE
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //IdTextBox.Text = Request.QueryString["id"];
            //NameTextBox.Text = Request.QueryString["name"];
            //AgeTextBox.Text = Request.QueryString["age"];

            if (Session["id"] != null && Session["name"] != null && Session["age"] != null && Session["img"] != null)
            {
                IdTextBox.Text = Session["id"].ToString();
                NameTextBox.Text = Session["name"].ToString();
                AgeTextBox.Text = Session["age"].ToString();
                Image1.ImageUrl = Session["img"].ToString();
            }
            else
            {
                Response.Redirect("WebForm1.aspx");
            }
        }
    }
}
Above File is DATA_SENDING_GRIDVIEW_ANOTHER_PAGE\WebForm2.aspx.cs














<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\DotNet\DATA_SENDING_GRIDVIEW_ANOTHER_PAGE\DATA_SENDING_GRIDVIEW_ANOTHER_PAGE\App_Data\Data_Send.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
ABOVE FILE IS Web.config FILE












CREATE TABLE [dbo].[tbl]
(
[Id] INT NOT NULL PRIMARY KEY,
[name] VARCHAR(50) NOT NULL,
[age] INT NOT NULL,
[image] VARCHAR(50) NOT NULL
)







Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

Display Auto Increment Value In Text Field After Insert To Database ASP.NET