Display selected ASP.Net DataList Item in GridView on Button Click using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

i want to add product into gridview from Datalist without checkbox sign and will use button add to card

here below my html

<table class="auto-style1">
    <tr>
        <td>
            <div>
                <asp:DataList ID="dlemp" runat="server" RepeatDirection="Horizontal" RepeatColumns="2">
                    <ItemTemplate>
                        <div id="pricePlans">
                            <ul id="plans">
                                <li class="plan">
                                    <ul class="planContainer">
                                        <li class="title">
                                            <h2>
                                                <asp:Label ID="ProductName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                                <br />
                                        </li>
                                        <li class="title">
                                            <asp:Image ID="imgEmp" Height="100px" Width="100px" runat="server" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' />
                                        </li>
                                        <li>
                                            <ul class="options">
                                                <div>
                                                    <li><b></b>
                                                        <asp:Label Visible="false" ID="CustomerID" runat="server" Text=' <%# Eval("Id") %>'></asp:Label>
                                                        <asp:Button ID="btnadd" runat="server" Text="Add Cart" OnClick="AddToCart"></asp:Button>
                                                    </li>
                                                </div>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                        </div>
                    </ItemTemplate>
                </asp:DataList>
            </div>
            &nbsp;
        </td>
        <td>
            Customer Name :
            <asp:TextBox ID="txtcustomer" runat="server"></asp:TextBox>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true">
                <Columns>
                    <asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <asp:Image ID="Data" runat="server" Width="100px" Height="80px" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label Text='<%#Eval("ID") %>' runat="server" ID="lbid" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label Text='<%#Eval("Name") %>' runat="server" ID="lblName" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="QTY">
                        <ItemTemplate>
                            <asp:TextBox ID="txtQty" runat="server" onkeyup="CalculateTotals();"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price">
                        <ItemTemplate>
                            <asp:Label Text='<%#Eval("Price") %>' runat="server" ID="lblPrice" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Total">
                        <ItemTemplate>
                            <asp:Label ID="lblTotal" Style="color: azure;" runat="server" Text="0"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Label ID="lblSumTotal" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="SubmitOrder" Height="29px" OnClick="Button1_Click" />
        </td>
    </tr>
</table>
Posted one month ago

Hi akhter,

Refer below sample.

HTML

<table class="auto-style1">
    <tr>
        <td>
            <div>
                <asp:DataList ID="dlemp" runat="server" RepeatDirection="Horizontal" RepeatColumns="2">
                    <ItemTemplate>
                        <div id="pricePlans">
                            <ul id="plans">
                                <li class="plan">
                                    <ul class="planContainer">
                                        <li class="title">
                                            <h2>
                                                <asp:Label ID="ProductName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                                <br />
                                        </li>
                                        <li class="title">
                                            <asp:Image ID="imgEmp" Height="100px" Width="100px" runat="server" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' />
                                        </li>
                                        <li>
                                            <ul class="options">
                                                <div>
                                                    <li><b></b>
                                                        <asp:Label Visible="false" ID="CustomerID" runat="server" Text=' <%# Eval("Id") %>'></asp:Label>
                                                        <asp:Button ID="btnadd" runat="server" Text="Add Cart" OnClick="AddToCart"></asp:Button>
                                                    </li>
                                                </div>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                        </div>
                    </ItemTemplate>
                    <FooterTemplate>
                    </FooterTemplate>
                </asp:DataList>
            </div>
            &nbsp;
        </td>
        <td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true">
                <Columns>
                    <asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <asp:Image ID="Data" runat="server" Width="100px" Height="80px" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label Text='<%#Eval("ID") %>' runat="server" ID="lbid" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label Text='<%#Eval("Name") %>' runat="server" ID="lblName" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="QTY">
                        <ItemTemplate>
                            <asp:TextBox ID="txtQty" runat="server" onkeyup="CalculateTotals();"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </td>
    </tr>
</table>

Namespaces

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string query = "SELECT top 4 * FROM tblfiles WHERE ContentType='image/pjpeg'";
        DataTable dt = GetData(query);
        dlemp.DataSource = dt;
        dlemp.DataBind();
    }
}

private static DataTable GetData(string query)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}

protected void AddToCart(object sender, EventArgs e)
{
    string id = "";
    Button btn = (Button)sender;
    DataListItem item = (DataListItem)btn.NamingContainer;
    id += (item.FindControl("CustomerID") as Label).Text;
    string query = "SELECT * FROM tblfiles where Id IN(" + id.TrimEnd(',') + ")";
    DataTable dt = GetData(query);
    if (Session["dt"] == null)
    {
        Session["dt"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    else
    {
        DataTable dt1 = Session["dt"] as DataTable;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt1.Rows.Add(dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3]);
        }
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

Screenshot