Click here to Skip to main content
14,827,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two cascading dropdownist(group and name) and enabled the autopostback true for the group dropdownlist...but when i enable the autopostback true for name dropdownlist...it doesnt let me select any other values in the name dropdownlist...it directly selects the first value and displays the first employee's designation...whereas when I disable the autopostback for the name dropdownlist it doesnt trigger any value for the label...where am i goin wrong?can anyone help me out. Below is the C# code


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.Configuration;


public partial class Forms_Master_Forms_Temporary_duty_form : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=ADE_2903_SOFTDEV; Integrated security = true");
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList7.Visible = false;

        if (!IsPostBack)
        {
            Getdata();//adding the division to the dropdown box
        }
    }

    /* Adding The contents to the Division DropDown List */
    
    private void Getdata()
    {
        SqlCommand cmd = new SqlCommand("SELECT DivisionID, DivShort FROM M_Division ORDER BY DivShort", conn);
        DataSet objDs = new DataSet();
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        conn.Open();
        sd.Fill(objDs);
        conn.Close();
        if (objDs.Tables[0].Rows.Count > 0)
         {
            ddlgroup.DataSource = objDs.Tables[0];
            ddlgroup.DataTextField = "DivShort";
            ddlgroup.DataValueField = "DivisionID";
            ddlgroup.DataBind();
            ddlgroup.Items.Insert(0, "--Select--");
         }
    }

    /*Adding the names to the Name DropDownList based on the selected division from Division Drop Down List*/

    private void FillName(int divisionID)
    {
        SqlCommand cmd = new SqlCommand("SELECT DivisionID, EmpName FROM M_Emp_Personal WHERE DivisionID =@DivisionID AND IsActive=1   ORDER BY EmpName", conn);
        cmd.Parameters.AddWithValue("@DivisionID", divisionID);
        DataSet objDs = new DataSet();
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        conn.Open();
        sd.Fill(objDs);
        conn.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            ddlname.DataSource = objDs.Tables[0];
            ddlname.DataTextField = "EmpName";
            ddlname.DataValueField = "DivisionID";
            ddlname.DataBind();
            ddlname.Items.Insert(0, "--Select--");
        }
    }

    /*Selecting the division present in the dropdownlist*/

    protected void ddlgroup_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int DivisionID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
            FillName(DivisionID);
            ddlname.SelectedIndex = 0;
     }

    /*Selecting the name present in the dropdownlist*/
    protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
    {
        int DesingID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
        FillDesig(DesingID);

    }

    private void FillDesig(int desingID)
    {
        SqlCommand cmd = new SqlCommand("Select M_Designation.DesigLong,M_Emp_Personal.EmpName From M_Designation inner join M_Emp_Personal on M_Designation.DesigID=M_Emp_Personal.DesigID", conn);
        cmd.Parameters.AddWithValue("@DesigID", desingID);
       
        conn.Open();
       
        lbldes.Text = cmd.ExecuteScalar().ToString();
        conn.Close();
    }

}

The ASP.NET code is

<pre lang="HTML"> <asp:DropDownList ID="ddlgroup" runat="server" 
                 
                
                style="z-index: 1; left: 118px; top: 148px; position: absolute; height: -5px;" 
                AutoPostBack="True" 
                onselectedindexchanged="ddlgroup_SelectedIndexChanged1">
            
            </asp:DropDownList>
<asp:DropDownList ID="ddlname" runat="server" 
               
                style="z-index: 1; left: 332px; top: 147px; position: absolute; height: 35px; width: 117px;" 
                onselectedindexchanged="ddlname_SelectedIndexChanged" AutoPostBack="True">
            </asp:DropDownList>

  <table style="width: 85%; removed 207px; removed 14px; removed: absolute; height: 593px;">
                <tr>
                    <td class="style5">
                        2.</td>
                    <td class="style7">
                        DESIGNATION</td>
                    <td colspan="3">
                        <asp:Label ID="lbldes" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
Posted
Updated 31-Jul-13 21:50pm
v4

Hi,

Can you please post your design code ? So I can dig more into it
   
Comments
Member 10066916 1-Aug-13 3:44am
   
sure...I have updated my question...
Member 10066916 1-Aug-13 3:51am
   
The ASP.NET code is

<pre lang="HTML"> <asp:DropDownList ID="ddlgroup" runat="server"


style="z-index: 1; left: 118px; top: 148px; position: absolute; height: -5px;"
AutoPostBack="True"
onselectedindexchanged="ddlgroup_SelectedIndexChanged1">


<asp:DropDownList ID="ddlname" runat="server"

style="z-index: 1; left: 332px; top: 147px; position: absolute; height: 35px; width: 117px;"
onselectedindexchanged="ddlname_SelectedIndexChanged" AutoPostBack="True">


<table style="width: 85%; removed 207px; removed 14px; removed: absolute; height: 593px;">
<tr>
<td class="style5">
2.</td>
<td class="style7">
DESIGNATION</td>
<td colspan="3">
<asp:Label ID="lbldes" runat="server" Text="Label">
</td>
</tr>
In the event : ddlgroup_SelectedIndexChanged1,

remove ddlname.SelectedIndex = 0;

Dont forget to rate the solution and mark it as an answer.

Regards,
CodeBlack
   
Comments
Member 10066916 1-Aug-13 8:55am
   
hey thanx for ur prompt reply...but it doesnt work....it goes on selecting the first name...
CodeBlack 2-Aug-13 3:15am
   
what exact functionality you want ?

Cascading Dropdown with updated label name based on child dropdown right ?
Member 10066916 4-Aug-13 12:54pm
   
yes..its like this...when an element from the group drop down list is clicked the name drop down list appears...when an element from name drop down list is clicked...the designation should appear into the label...
CodeBlack 5-Aug-13 0:20am
   
Hi, Please have a look at the below code :

#region Namespace
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;
using System.Globalization;
#endregion

public partial class Forms_Master_Forms_Temporary_duty_form : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=ADE_2903_SOFTDEV; Integrated security = true");

private List<employee> EmployeeData;

protected void Page_Load(object sender, EventArgs e)
{
// DropDownList7.Visible = false;

if (!IsPostBack)
{
Getdata();//adding the division to the dropdown box
}
}

/* Adding The contents to the Division DropDown List */

private void Getdata()
{
for (int i = 1; i < 4; i++)
{
ddlgroup.Items.Add(new ListItem("Grop" + i, i.ToString()));
}

ddlgroup.Items.Insert(0, "--Select--");
}

/*Adding the names to the Name DropDownList based on the selected division from Division Drop Down List*/

private void FillName(int divisionID)
{


var data = this.MyData().Where(e => e.EmployeeDivision == divisionID).ToList();

ddlname.DataSource = data;
ddlname.DataValueField = "EmployeeId";
ddlname.DataTextField = "EmployeeName";
ddlname.DataBind();
ddlname.Items.Insert(0, "--Select--");
}

/*Selecting the division present in the dropdownlist*/

protected void ddlgroup_SelectedIndexChanged1(object sender, EventArgs e)
{
int DivisionID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
FillName(DivisionID);
// ddlname.SelectedIndex = 0;
}

/*Selecting the name present in the dropdownlist*/
protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
{
int DesingID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
FillDesig(DesingID);

}

private void FillDesig(int desingID)
{
// SqlCommand cmd = new SqlCommand("Select M_Designation.DesigLong,M_Emp_Personal.EmpName From M_Designation inner join M_Emp_Personal on M_Designation.DesigID=M_Emp_Personal.DesigID", conn);
// cmd.Parameters.AddWithValue("@DesigID", desingID);
//
// conn.Open();

lbldes.Text = desingID.ToString();
// conn.Close();
}

private List<employee> MyData()
{
EmployeeData = new List<employee>();
EmployeeData.Add(new Employee()
{
EmployeeDivision = 1,
EmployeeId = "1",
EmployeeName = "aaa"
});

EmployeeData.Add(new Employee()
{
EmployeeDivision = 2,
EmployeeId = "2",
EmployeeName = "bbb"
});

EmployeeData.Add(new Employee()
{
EmployeeDivision = 3,
EmployeeId = "3",
EmployeeName = "ccc"
});

return EmployeeData;
}

}

public class Employee
{
public string EmployeeName { get; set; }

public string EmployeeId { get; set; }

public int EmployeeDivision { get; set; }
}

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