Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to insert,update and delete an images using file upload control in gridview?
Posted

1 solution

XML
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h2>
Please Select A Brand To See its Laptop Models</h2>
<table class="style20">
<tr>
<td class="style16">
<asp:Label ID="BrandNameLabel" runat="server" Text="Brand Name"></asp:Label>
</td>
<td class="style17">
<asp:DropDownList ID="BrandNameDropDownList" runat="server" AutoPostBack="True"
Height="22px" Width="155px"
onselectedindexchanged="BrandNameDropDownList_SelectedIndexChanged">
<asp:ListItem Text="Select something" Value="-1" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style16">
<asp:Label ID="ProductNameLabel" runat="server" Text="Product Name"></asp:Label>
</td>
<td class="style17">
<asp:DropDownList ID="ProductNameDropDownList" runat="server" Width="157px"
AutoPostBack="True"
ondatabinding="ProductNameDropDownList_DataBinding"
ontextchanged="ProductNameDropDownList_TextChanged">
<asp:ListItem Text="Select something" Value="-1" />
</asp:DropDownList>
</td>
</tr>
</table>
<asp:GridView ID="GridView" runat="server" BackColor="White"
BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
GridLines="Horizontal" AutoGenerateColumns="False" AllowPaging="True"
PageSize="1" Width="819px" ForeColor="#003366"
AutoGenerateEditButton="True" onrowediting="GridView_RowEditing">
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Specification">
<ItemTemplate>
<asp:Label ID="Specificationlabel" runat="server" Text='<% # Eval("Specification") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="PriceLabel" runat="server" Text='<% # Eval("Price") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Instock">
<ItemTemplate>
<asp:Label ID="InstockLabel" runat="server" Text='<% # Eval("Instock") %>' />
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
<br />
<br />
<br />
<br />
</asp:Content>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class BrandSearchLaptop : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectioninfo"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
SqlCommand cmd1 = new SqlCommand("Select * from LaptopBrandTable", con);
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
BrandNameDropDownList.Items.Add(dr1["BrandName"].ToString());
}
con.Close();
}
}
protected void BrandNameDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
ProductNameDropDownList.Items.Clear();
String A=BrandNameDropDownList.SelectedValue;
string B = ProductNameDropDownList.SelectedValue;
if (B == "No Product Exists..")
{
GridView.DataSource = null;
GridView.DataBind();
}
else
{
con.Open();
SqlCommand cmd2 = new SqlCommand("Select * from LaptopTable Where BrandName='" + A + "'", con);
SqlDataReader dr2 = cmd2.ExecuteReader();
ProductNameDropDownList.Items.Add("Please Select");
if (dr2.HasRows == true)
{
while (dr2.Read())
//Names collection is a combo box.
ProductNameDropDownList.Items.Add(dr2["ProductName"].ToString());
}
else
{
ProductNameDropDownList.Items.Clear();
ProductNameDropDownList.Items.Add("No Product Exists..");
GridView.DataSource = null;
GridView.DataBind();
//PriceTextBox.Text = string.Empty;
}
con.Close();
}

}
protected void ProductNameDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid();
}
void BindGrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from LaptopTable where ProductName='" + ProductNameDropDownList.SelectedItem + "'and BrandName='" + BrandNameDropDownList.SelectedItem + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView.DataSource = ds;
GridView.DataBind();
}
protected void ProductNameDropDownList_DataBound(object sender, EventArgs e)
{
}
protected void ProductNameDropDownList_DataBinding(object sender, EventArgs e)
{
}
protected void ProductNameDropDownList_TextChanged(object sender, EventArgs e)
{
BindGrid();
}
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditIndex;
BindGrid();
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900