This way:
HTML
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtId" runat="server" OnTextChanged="Search" AutoPostBack="true" />
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceHolder1"
GroupPlaceholderID="groupPlaceHolder1">
<LayoutTemplate>
<table cellpadding="2" cellspacing="2">
<tr>
<th>
Prouct Name
</th>
<th>
Qty
</th>
<th>
Price
</th>
<th>
VAT
</th>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("ProductName")%>
</td>
<td>
<%# Eval("Quantity")%>
</td>
<td>
<%# Eval("Price")%>
</td>
<td>
<%# Eval("Vat")%>
</td>
</ItemTemplate>
</asp:ListView>
</div>
</form>
Namespace
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
C#
protected void Search(object sender, EventArgs e)
{
BindListView();
}
private void BindListView()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.CommandText = "SELECT * FROM tblProduct WHERE Id = @Id";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(this.txtId.Text.Trim()));
DataTable dt = new DataTable();
sda.Fill(dt);
DataTable products = new DataTable();
if (ViewState["Products"] == null)
{
products.Merge(dt, true);
}
else
{
products = (DataTable)ViewState["Products"];
products.Merge(dt, true);
}
ViewState["Products"] = products;
ListView1.DataSource = products;
ListView1.DataBind();
}
}
}
}
Screenshot
