Drop Down Menu Functionality
Hi friends,
I have got a small problem here with my program here. I am working in ASP.NET and C# using Microsoft Visual Studio .NET v1.1. Well I am using 2 drop down menus and when I select one item from the 1st Drop-down box the second drop down should get populated. I will give u all finer details along with the code also.
I have 2 tables in the database: DivisionMaster and DepartmentMaster. The DivisionMaster table consists of DivisionId and DivisionName. The DepartmentMaster table consists of DivisionID and DepartmentName. The DivisionNames are getting displayed in the ddlempdiv(DropDownList1) and on the selection of the DivisionName from ddlempdiv,the DepartmentNames under taht particular DivisionId should get displayed in ddlempdep(DropDownList2). The logic behind the code is,that when I select the DivisionName from the DDlist 1 the DivisionName should also be selected and as per that DivisionId,the respective DepartmentName should be returned.
Incase u all feel that my code is long or confusing,please incase u all have a better way of coding the above,please give ur suggesstions. Thanlks in advance.
I am displaying the code below :
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Web.Configuration;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Common;
using System.Text;
using System.IO;
namespace Trial
{
/// <summary>
/// Summary description for employeemaster.
/// </summary>
public class employeemaster : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Button btnadd;
protected System.Web.UI.WebControls.Button btnclose;
protected System.Web.UI.WebControls.DropDownList ddlempdep;
protected System.Web.UI.WebControls.TextBox txtcode;
protected System.Web.UI.WebControls.TextBox txtpwd;
protected System.Web.UI.WebControls.TextBox txtname;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvename;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvecode;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvepwd;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvddledept;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvddlediv;
protected System.Web.UI.WebControls.Label lblecode;
protected System.Web.UI.WebControls.Label lblename;
protected System.Web.UI.WebControls.Label lblepwd;
protected System.Web.UI.WebControls.DropDownList ddlempdiv;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnadd.Click += new System.EventHandler(this.btnadd_Click);
this.btnclose.Click += new System.EventHandler(this.btnclose_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnadd_Click(object sender, System.EventArgs e)
{
}
static string id=null;
string id1=id;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
if(txtcode.Text.Length>50)
lblecode.Text="Your code has to be less than 50 characters";
if(txtname.Text.Length>50)
lblename.Text="Your name has to be less than 50 characters";
if(txtpwd.Text.Length>50)
lblepwd.Text="Your password has to be less than 50 characters";
Trial_Class Trial_Object;
Trial_Object=(Trial_Class)Session["session_Trial_Object"];
string connectionstring = (string)ConfigurationSettings.AppSettings["Conn"];
SqlConnection myConnection = new SqlConnection(connectionstring);
string query = "select * from DivisionMaster";
myConnection.Open();
SqlCommand cmd = new SqlCommand(query,myConnection);
SqlDataReader myReader = null;
myReader = cmd.ExecuteReader();
ddlempdiv.Items.Add("Select");
while(myReader.Read())
{
ddlempdiv.Items.Add(myReader["DivisionName"].ToString());
-----> id=(myReader["DivisionId"].ToString());
}
myReader.Close();
myConnection.Close();
}
else
{
empdept(sender,e);
}
}
public void empdept(object sender, System.EventArgs e)
{
string connectionstring = (string)ConfigurationSettings.AppSettings["Conn"];
SqlConnection myConnection = new SqlConnection(connectionstring);
string query = "select * from DepartmentMaster,DivisionMaster where DepartmentMaster.DivisionId=" +id1;
myConnection.Open();
SqlCommand cmd = new SqlCommand(query,myConnection);
SqlDataReader myReader = null;
myReader = cmd.ExecuteReader();
ddlempdep.Items.Add("Select");
while(myReader.Read())
{
ddlempdep.Items.Add(myReader["DepartmentName"].ToString());
}
myReader.Close();
myConnection.Close();
}
private void btnclose_Click(object sender, System.EventArgs e)
{
rfvecode.Enabled = false;
rfvename.Enabled = false;
rfvepwd.Enabled = false;
rfvddledept.Enabled = false;
rfvddlediv.Enabled = false;
btnclose.Attributes.Add("onclick","window.close(); ");
}
}
}
|