Look into the following Sample:
Here I took one drop downlist with values 10, 20, 30, 40 and 50 as department IDs and also one GridView for data binding based on DropDownList selected value
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewWithDropDownList.aspx.cs" Inherits="TextBoxValidation.GridViewWithDropDownList" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" AutoPostBack="true" ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="10" />
<asp:ListItem Text="20" />
<asp:ListItem Text="30" />
<asp:ListItem Text="40" />
<asp:ListItem Text="50" />
</asp:DropDownList>
<asp:GridView runat="server" ID="gdvEmp"/>
</div>
</form>
</body>
</html>
aspx.cs Code 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;
namespace TextBoxValidation
{
public partial class GridViewWithDropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData(DropDownList1.SelectedValue);
}
}
private void GetData(string ID){
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("Data source=XXXX ; Initial Catalog=study ; Integrated Security=true");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Employees WHERE Department_ID= @ID", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@ID",ID);
//sqlDa.Fill(dt);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
gdvEmp.DataSource = ds;
gdvEmp.DataBind();
connection.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GetData(DropDownList1.SelectedItem.Value);
}
}
}
Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif
Saanjhi, if this helps please login to Mark As Answer. | Alert Moderator