Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2006, 02:29 AM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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(); ");
        }
    }
}


 
Old January 13th, 2006, 02:51 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Set autopostback to TRUE on ddl1. Then in the onselectedindexchanged event, grab the selected id. Use that in a sql statment to pull deptnames based on that Id, then fill ddl2 with the names.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop down menu mizlatic BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 February 11th, 2008 07:02 AM
Drag n drop functionality anujrathi ASP.NET 2.0 Professional 0 February 13th, 2007 08:01 AM
drop-down menu crmpicco Javascript How-To 1 March 22nd, 2005 01:23 PM
right click menu hidden by drop-drown menu Andraw HTML Code Clinic 0 March 18th, 2005 03:28 PM
drop down menu junemo Beginning PHP 2 May 7th, 2004 10:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.