Friday, 2 May 2014

Datalist example

No comments
user_product.aspx

<asp:DataList ID="datalistall" runat="server" RepeatColumns="3"
              RepeatDirection="Horizontal" Width="100%" DataKeyField="p_id" >
          <ItemTemplate>
          <table>
          <tr>
          <td style="border-style:dashed; border-width:thin; width:200px;">   
              <asp:ImageButton ID="imgbtn" runat="server" ImageUrl='<%# "~/img//"+Eval("Image_url") %>'/>                           
              <br />            
              Name:
              <asp:Label ID="lblpname" runat="server"
                  Text='<%# Eval("Product_name") %>' Font-Bold="True" Font-Size="Larger" />                                     
              <br />
              Brand:
              <asp:Label ID="lblbrand" runat="server" Text='<%# Eval("Brand") %>' Font-Bold="True" Font-Size="Larger"/>
                  <br />
              Description:
              <asp:Label ID="lbldesc" runat="server"
                  Text='<%# Eval("Description") %>' Font-Bold="True" Font-Size="Larger"/>
              <br />           
           <asp:Label ID="lblerror" runat="server" Text="No. of items is blank" Font-Bold="True" Font-Size="Larger" Visible="false" ForeColor="Red"/>
           <br />
<asp:Label ID="lbladded" runat="server" Text="Added successfully" Font-Bold="True" Font-Size="Larger" Visible="false" ForeColor="Red"/>
           <br />
            Enter The Quantity:&nbsp;
            <asp:TextBox ID="TextBox1" runat="server" Height="29px" Width="48px"></asp:TextBox>
            &nbsp;&nbsp;
            <br />
          <asp:Button ID="Button1" runat="server"  Text="Add to Cart" OnClick="add_cart"/>
          </td>
          </tr>
          </table>
          </ItemTemplate>
          </asp:DataList>


.cs

 protected void add_cart(object sender, EventArgs e)
    {
        Button bt = (Button)sender;
        DataListItem dlist = (DataListItem)bt.Parent;

        Label error = (Label)dlist.FindControl("lblerror");
        Label success = (Label)dlist.FindControl("lbladded");
        ImageButton img = (ImageButton)dlist.FindControl("imgbtn");     
        Label pname = (Label)dlist.FindControl("lblpname");  
        Label brand = (Label)dlist.FindControl("lblbrand");
        TextBox items = (TextBox)dlist.FindControl("TextBox1");

        DataTable dt = new DataTable();

        if (items.Text == "")
        {
            error.Visible = true;
            success.Visible = false;
        }
 else
        {
            if (Session["usename"] != null)
            {
                error.Visible = false;
                if (Session["cart"] != null)
                {
                    dt = (DataTable)Session["cart"];
                }
                else
                {                
                    DataColumn Product_name = new DataColumn("Product_name");
                    DataColumn Category = new DataColumn("Category");
                    DataColumn Brand = new DataColumn("Brand");                 
                    DataColumn qty = new DataColumn("qty");                
                    DataColumn image = new DataColumn("image");
                    DataColumn tprice = new DataColumn("tprice");
                    DataColumn Price = new DataColumn("Price");

                    dt.Columns.Add(Product_name);
                    dt.Columns.Add(Category);
                    dt.Columns.Add(Brand);                 
                    dt.Columns.Add(qty);                  
                    dt.Columns.Add(image);
                    dt.Columns.Add(tprice);
                    dt.Columns.Add(Price);
                }

                DataRow dr = dt.NewRow();
           
                dr["Product_name"] = pname.Text;
                dr["Category"] = cat.Text;
                dr["Brand"] = brand.Text;
                dr["qty"] = items.Text;
                dr["image"] = img.ImageUrl;
                dr["Price"] = price.Text;
                Decimal ttl = Decimal.Parse(price.Text) * Decimal.Parse(items.Text);
                dr["tprice"] = ttl;
                dt.Rows.Add(dr);

                Session["cart"] = dt;
                Response.Redirect(string.Format("user_product.aspx?pi={0}&ct={1}", pid.Text, cat.Text));
                success.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }