Pass (Send) GridView selected row to another page on row Click using C# and VB.Net in ASP.Net

George616
 
on Oct 14, 2020 06:02 AM
Sample_432525.zip
1042 Views

Hello Forum,

I want to achieve a situation where I can pass data in a row in gridview from any web form and display the details in only one web form. I am trying to avoid having too many web forms.

For example, I have 4 web forms (webform1, webform2, webform3 and webform4), and I have one detail web form where the data will be displayed. Then when I click on a row in the gridview, then that row data will be passed to the detail webform. When I select a row from gridview in webform1, then data will be passed to detail webform. Also when I click on a gridview row in webform2, data will also be passed to that same detail webform but different data will be shown because it comes from different gridview and different webforms. The same will be for webform3 and webform4.

And in detail webform, how can I arrange the way I want the details to display? 

HTML

            <asp:GridView ID="GridView1" runat="server" GridLines="None" AllowPaging="true" HeaderStyle-ForeColor="#00003D" HeaderStyle-Font-Bold="true"
                AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged" class="table" Width="100%">
                <EmptyDataTemplate>
                   <div style="text-align: center; font-weight: bolder; font-size: medium;">
                       <asp:Label ID="labelTemp" runat="server" Text="No Record"></asp:Label>
                   </div>
               </EmptyDataTemplate>
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="File Name" />
                    <asp:BoundField DataField="Organization" HeaderText="Name of Organization" />
                    <asp:BoundField DataField="CreatedBy" HeaderText="Created By" />
                    <asp:BoundField DataField="CreatedDate" HeaderText="Date Created" />
                </Columns>
            </asp:GridView>

C#

 protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Click to select this row.";
        }
    }

    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowIndex == GridView1.SelectedIndex)
            {
                Response.Redirect("Detail.aspx?Name=" + row.Cells[0].Text.Trim());
            }
        }
    }

Detail web form 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;
using System.Data.SqlClient;
using System.Web.Security;

public partial class Detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Request.QueryString["Name"]);
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 15, 2020 03:34 AM
on Oct 15, 2020 03:47 AM

Hi George616,

Check this example. Now please take its reference and correct your code.

HTML

Default

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="2"
    OnPageIndexChanging="OnPageIndexChanging" OnRowDataBound="OnRowDataBound" DataKeyNames="Id"
    OnSelectedIndexChanged="OnSelectedIndexChanged">
    <EmptyDataTemplate>
        <div style="text-align: center; font-weight: bolder; font-size: medium;">
            <asp:Label ID="labelTemp" runat="server" Text="No Record"></asp:Label>
        </div>
    </EmptyDataTemplate>
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="File Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Detail

Id:<asp:Label ID="lblId" runat="server" /><br />
Name:<asp:Label ID="lblName" runat="server" /><br />
Country:<asp:Label ID="lblCountry" runat="server" />

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

Default

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Click to select this row.";
    }
}

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            Server.Transfer("Detail.aspx?RowIndex=" + row.RowIndex);
        }
    }
}

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}

private void BindGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

Detail

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
        GridViewRow row = GridView1.Rows[rowIndex];
        lblId.Text = GridView1.DataKeys[rowIndex].Value.ToString();
        lblName.Text = row.Cells[0].Text;
        lblCountry.Text = row.Cells[1].Text;
    }
}

VB.Net

Default

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex)
        e.Row.ToolTip = "Click to select this row."
    End If
End Sub

Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    For Each row As GridViewRow In GridView1.Rows
        If row.RowIndex = GridView1.SelectedIndex Then
            Server.Transfer("Detail.aspx?RowIndex=" & row.RowIndex)
        End If
    Next
End Sub

Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    GridView1.PageIndex = e.NewPageIndex
    Me.BindGrid()
End Sub

Private Sub BindGrid()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Detail

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Me.Page.PreviousPage IsNot Nothing Then
        Dim rowIndex As Integer = Integer.Parse(Request.QueryString("RowIndex"))
        Dim GridView1 As GridView = CType(Me.Page.PreviousPage.FindControl("GridView1"), GridView)
        Dim row As GridViewRow = GridView1.Rows(rowIndex)
        lblId.Text = GridView1.DataKeys(rowIndex).Value.ToString()
        lblName.Text = row.Cells(0).Text
        lblCountry.Text = row.Cells(1).Text
    End If
End Sub

Screenshot