exaple of datalist using asp.net with C#

Design.aspx



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="4">
            <ItemTemplate><asp:Panel ID="Panel1" runat="server" BorderColor="#FF9933"
                BorderWidth="3px" Height="380px" Width="270px">
    <table height="150" >
    <tr >
        <td width="75%" style="color: #FF0000; font-weight: bold" align="center">
            <span style="color: Black; font-weight: bold;">Product image:</span><br />
            <asp:Label ID="Label1" runat="server" Visible="false" Text='<%#Eval("pro_id") %>' ></asp:Label>
        </td>
    </tr>
    <tr >
        <td width="75%" style="color: #FF0000; font-weight: bold" align="center">
            <span style="color: Black; font-weight: bold;">Product image:</span><br />
            <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("pro_image") %>' Height="90px" Width="90px" ></asp:Image>
        </td>
    </tr>
    <tr >
        <td width="75%" style="color: #0000FF; font-weight: bold">
            <span style="color: Black; font-weight: bold;">Produc name:</span><br />
            <asp:Label ID="lbl" runat="server" Text='<%# Eval("pro_name") %>'></asp:Label>
        </td>
    </tr>
    <tr >
        <td width="50%" style="color: #009900; font-weight: bold">
            <span style="color: Black; font-weight: bold;">Product price:</span><br />
            <asp:Label ID="lbl2" runat="server" Text='<%#Eval("pro_price") %>'></asp:Label>
        </td>
    </tr>
    <tr >
        <td width="75%" style="color: #FF0000; font-weight: bold"><span style="color: Black; font-weight: bold;">quantity:</span>
        <br /><asp:Label ID="lbl3" runat="server" Text='<%#Eval("pro_quantity") %>'></asp:Label>
        </td>
    </tr>
    <tr>
        <td align="Right">
            <asp:LinkButton ID="LinkButton1" runat="server"
            Font-Underline="False" style="font-weight: 700; color: Black"
            CommandName="ViewDetails" CommandArgument='<%#Eval("pro_id") %>'
            BackColor="#FF9933" onclick="LinkButton1_Click">ViewDeatils</asp:LinkButton>
        </td>
    </tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>

Design.aspx.cs



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.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
        SqlConnection conn = new SqlConnection(con);
        string q = "select pro_id,pro_image,pro_name,pro_price,pro_quantity from pi_product";

        SqlDataAdapter da = new SqlDataAdapter();
        DataTable ds = new DataTable();
        SqlCommand cmd1 = new SqlCommand(q, conn);
        conn.Open();
        da.SelectCommand = cmd1;

        cmd1.ExecuteNonQuery();
        da.Fill(ds);
        DataList1.DataSource = ds;
        DataList1.DataBind();

        conn.Close();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["id"] = ((LinkButton)sender).CommandArgument;
        Response.Redirect("showdetail.aspx");
    }
       
}




showDetail.aspx



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showdetail.aspx.cs" Inherits="showdetail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 369px;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
   
    <div>
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:Image ID="Image1" runat="server" Height="150px" Width="150px" />
                 </td><td>
                 <table class="style1">
            <tr>
                <td style="color: #0000FF; font-weight: 700" >
                    <span style="color: Black; font-weight: bold;">Modal:</span><br /><asp:Literal ID="Literal1" runat="server"></asp:Literal>
                   
                </td>
            </tr>
            <tr>
                <td style="font-weight: 700; color: #009933" >
                    <span style="color: Black; font-weight: bold;">Producname:</span><br /><asp:Literal ID="Literal2" runat="server"></asp:Literal>
                </td>
            </tr>
            <tr>
                <td style="font-weight: 700; color: #FF0000" >
                    <span style="color: Black; font-weight: bold;">Price:</span><br /><asp:Literal ID="Literal3" runat="server"></asp:Literal>
                </td>
            </tr>
            <tr>
                <td style="font-weight: 700; color: #FF0000" >
                    <span style="color: Black; font-weight: bold;">quantity:</span><br /><asp:Literal ID="Literal4" runat="server"></asp:Literal>
                </td>
            </tr>
                </table>
                </td>
            </tr>
        </table>
        <asp:Button ID="Button1" runat="server" Text="Back" onclick="Button1_Click" />
    </div>
    <div>
       
    </div>
    </form>
</body>
</html>


showdetail.aspx.cs


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;

public partial class showdetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        databind();
    }
    public void databind()
    {
        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
        SqlConnection conn = new SqlConnection(con);
        string q = "select pro_image,pro_name,pro_price,pro_quantity from pi_product where pro_id='" + Session["id"].ToString() + "' ";

        SqlCommand cmd1 = new SqlCommand(q, conn);
        conn.Open();

        SqlDataReader dr = cmd1.ExecuteReader();
        while (dr.Read())
        {
            Image1.ImageUrl = dr[0].ToString();
            Literal2.Text = dr[1].ToString();
            Literal3.Text = dr[2].ToString();
            Literal4.Text = dr[3].ToString();
          
        }
        conn.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}


0 comments :