Here I have created sample that shows the product list when you click on product Name the Details of that product will be shown in table.
HTML
<div>
<asp:DataList ID="dlProducts" runat="server" RepeatColumns="4">
<HeaderTemplate>
<div>
<span>Products</span>
</HeaderTemplate>
<ItemTemplate>
<div style="border: 1px solid #000; padding: 5px; float: inherit; text-align: center;
text-decoration: none">
<asp:LinkButton ID="LinkButton1" Text='<%# Eval("ProductName") %>' OnClick="ShowProductDetails"
runat="server" /></div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:DataList>
</div>
<div>
<table id="tblProduct" runat="server" border="0" cellpadding="0" cellspacing="0"
visible="false">
<tr>
<td colspan="2" style="text-align: center">
<asp:Label Text="Product Details" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label Text="Product Id" runat="server" />
</td>
<td>
<asp:Label ID="lblProductId" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label Text="Product Name" runat="server" />
</td>
<td>
<asp:Label ID="lblProductName" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label Text="Product Price" runat="server" />
</td>
<td>
<asp:Label ID="lblProductPrice" runat="server" />
</td>
</tr>
</table>
</div>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetProducts();
}
}
protected void ShowProductDetails(object sender, EventArgs e)
{
LinkButton productName = (sender as LinkButton);
this.GetProductDetails(productName.Text);
this.tblProduct.Visible = true;
}
private void GetProducts()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Top 10 ProductName FROM Products", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dtProducts = new DataTable();
sda.Fill(dtProducts);
this.dlProducts.DataSource = dtProducts;
this.dlProducts.DataBind();
}
}
}
}
private void GetProductDetails(string productName)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT ProductId,ProductName,UnitPrice FROM Products WHERE ProductName = @ProductName", con))
{
cmd.Parameters.AddWithValue("@ProductName", productName);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dtProduct = new DataTable();
sda.Fill(dtProduct);
foreach (DataRow product in dtProduct.Rows)
{
this.lblProductId.Text = product["ProductId"].ToString();
this.lblProductName.Text = product["ProductName"].ToString();
this.lblProductPrice.Text = product["UnitPrice"].ToString();
}
}
}
}
}
Screenshot
