Hi rajeesh,
Check this example. Now please take its reference and correct your code.
HTML
Master Page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<span style="float: right">Items in Cart : <asp:Label ID="label1" runat="server"
Text="0" /></span>
<br />
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Login Page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Login Page
<hr />
<asp:Button Text="Login" runat="server" PostBackUrl="~/Products.aspx" />
</asp:Content>
Products Page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Products Page
<hr />
<asp:Button Text="View Details" runat="server" PostBackUrl="~/ProductDetails.aspx" />
</asp:Content>
Product Details Page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Product Details Page
<hr />
<asp:GridView ID="gvCardDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CardNumber" HeaderText="CardNumber" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageUrl="~/AddCard.png" AlternateText="Add To Cart"
runat="server" OnClick="Add" Height="25px" Width="25px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnContinue" Text="Continue Shopping" runat="server" />
<br />
Cart Items :<br />
<asp:Label ID="Label43" runat="server" />
</asp:Content>
Code
Product Details Page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("CardNumber");
dt.Rows.Add(123344);
dt.Rows.Add(23562674);
dt.Rows.Add(5566323);
dt.Rows.Add(34545453);
this.gvCardDetails.DataSource = dt;
this.gvCardDetails.DataBind();
}
}
protected void Add(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)((sender) as ImageButton).NamingContainer;
Session["CardDetails"] += row.Cells[0].Text + Environment.NewLine;
if (Session["CardDetails"] != null)
{
string[] cartItems = Session["CardDetails"].ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
// Accessing Master Page Label and assigning the value to it.
((Label)this.Master.FindControl("label1")).Text = (cartItems.Length - 1).ToString();
Label43.Text = Session["CardDetails"].ToString().Replace(Environment.NewLine, "<br/>");
}
}
Screenshot