Hey jon,
Please refer below smaple.
HTML
CS.aspx
<asp:GridView ID="GV_ordini" runat="server" DataKeyNames="CustomerId" OnRowCreated="GV_ordini_RowCreated">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnDettCollez" runat="server" CausesValidation="false" CommandArgument='<%# Container.DataItemIndex %>'
CommandName="Select" Text="collez" OnClick="btnDettCollez_Click" />
</ItemTemplate>
<ControlStyle CssClass="btn btn-info" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx
<div>
<asp:GridView runat="server" ID="gvCustomers">
</asp:GridView>
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
this.GV_ordini.DataSource = dt;
this.GV_ordini.DataBind();
}
}
}
}
}
protected void btnDettCollez_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
Session["id"] = row.Cells[1].Text;
Response.Redirect("Default.aspx");
}
protected void button_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('You have clicked " + ((sender as Button).Parent.Controls[0] as Literal).Text + " Column.')", true);
}
protected void GV_ordini_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < e.Row.Cells.Count; i++)
{
Literal lit = new Literal();
lit.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Controls.Add(lit);
Button btn = new Button();
btn.Text = "Submit";
btn.ID = "btnId" + i;
btn.CommandName = "Select";
btn.Click += new EventHandler(this.button_Click);
e.Row.Cells[i].Controls.Add(btn);
}
}
}
Default.asp.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string id = Session["id"].ToString();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId=@CustomerId", con))
{
cmd.Parameters.AddWithValue("@CustomerId", id);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
}
}
Vb.Net
VB.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
Me.GV_ordini.DataSource = dt
Me.GV_ordini.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub btnDettCollez_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)
Session("id") = row.Cells(1).Text
Response.Redirect("Default.aspx")
End Sub
Protected Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('You have clicked " & (TryCast((TryCast(sender, Button)).Parent.Controls(0), Literal)).Text & " Column.')", True)
End Sub
Protected Sub GV_ordini_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
For i As Integer = 1 To e.Row.Cells.Count - 1
Dim lit As Literal = New Literal()
lit.Text = e.Row.Cells(i).Text
e.Row.Cells(i).Controls.Add(lit)
Dim btn As Button = New Button()
btn.Text = "Submit"
btn.ID = "btnId" & i
btn.CommandName = "Select"
AddHandler btn.Click, AddressOf button_Click
e.Row.Cells(i).Controls.Add(btn)
Next
End If
End Sub
Default.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim id As String = Session("id").ToString()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE CustomerId=@CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", id)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Screenshot