Search code examples
c#asp.netdrop-down-menutextbox

How to display fixed data on textbox based on cascading dropdown list in C#?


I would like to know how can I display fixed data, based on the cascading dropdown list the user chooses? For example, I have 3 dropdown lists, on the last dropdown list if the user chooses "Yes", textbox will display "Position is approved" and if the user chooses "No", textbox will display "Position not approved". This is my code. I don't know what else to, please help.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FillPackage();

    }
}


private void FillPackage()
{
    string strConn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();

    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT TopID, ToPackage FROM TableTop1";
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();

    if (objDs.Tables[0].Rows.Count > 0)
    {
        ddl1.DataSource = objDs.Tables[0];
        ddl1.DataTextField = "ToPackage";
        ddl1.DataValueField = "TopID";
        ddl1.DataBind();
        ddl1.Items.Insert(0, "--Select--");

    }
    else
    {
        lblMsg.Text = "No Package found";
    }
}

private void FillPurpose(int TopID)
{
    string strConn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();

    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT PurposeID, Purpose FROM TablePurpose1 WHERE TopID =@TopID";
    cmd.Parameters.AddWithValue("@TopID", TopID);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();

    if (objDs.Tables[0].Rows.Count > 0)
    {
        ddl2.DataSource = objDs.Tables[0];
        ddl2.DataTextField = "Purpose";
        ddl2.DataValueField = "PurposeID";
        ddl2.DataBind();
        ddl2.Items.Insert(0, "--Select--");
    }
    else
    {
        lblMsg.Text = "Please consult with focal person";
    }
}

private void FillReason(int PurposeID)
{
    string strConn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT ReasonID, Reason FROM TableReason1 WHERE PurposeID =@PurposeID";
    cmd.Parameters.AddWithValue("@PurposeID", PurposeID);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();

    if (objDs.Tables[0].Rows.Count > 0)
    {
        ddl3.DataSource = objDs.Tables[0];
        ddl3.DataTextField = "Reason";
        ddl3.DataValueField = "ReasonID";
        ddl3.DataBind();
        ddl3.Items.Insert(0, "--Select--");
    }
    else
    {
        lblMsg.Text = "Please consult with focal person";
    }
}

Solution

  • Since you have a Page_Load method in your snippet I'll consider your are using asp.net web forms. If that is the case you just have to handle the event emitted by the DropDownList component when it is changed.

    I'm your c# code you should create a method to handle the event of an item being selected in your DropDownList, something like this:

    protected void ItemSelected(object sender, EventArgs e)
    {
        if (ddl3.SelectedItem.Value == "Yes")
        {
            // Change text box text
        }
    }
    

    And then you just have to hook your event handler to the component:

    <asp:DropDownList ID="ddl3" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ItemSelected">
        </asp:DropDownList>