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

 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

{

    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()

        {

            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();

        }


        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\WebForm1.aspx.cs





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DATA_SENDING.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" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" 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" Height="100" Width="100" runat="server" 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\WebForm1.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
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TextBox1.Text = Request.QueryString["id"];
            //TextBox2.Text = Request.QueryString["name"];
            //TextBox3.Text = Request.QueryString["age"];

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









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

<!DOCTYPE html>

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




















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