Hi suhaas121,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnadd" OnClick="btnadd_Click" runat="server" Text="Add To Cart" />
Namespaces
C#
using System.Data;
using System.Text;
VB.Net
Imports System.Data
Imports System.Text
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
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();
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
StringBuilder strb = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if (isChecked)
{
strb.Append(GridView1.Rows[i].Cells[1].Text).AppendLine();
}
}
Session["vendor"] = strb.ToString().Trim();
Response.Redirect("Order.aspx");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
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 If
End Sub
Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strb As StringBuilder = New StringBuilder()
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
Dim isChecked As Boolean = (CType(row.FindControl("CheckBox1"), CheckBox)).Checked
If isChecked Then
strb.Append(GridView1.Rows(i).Cells(1).Text).AppendLine()
End If
Next
Session("vendor") = strb.ToString().Trim()
Response.Redirect("Order.aspx")
End Sub
Order Page
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Session["vendor"] != null)
{
string[] vendors = Session["vendor"].ToString().Split('\n');
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("vendor") IsNot Nothing Then
Dim vendors As String() = Session("vendor").ToString().Split(vbLf)
End If
End Sub