Darren
Darren

Reputation: 793

Updating a databound dropdownlist control when another databound dropdownlist changes

I have two databound dropdown lists:

if (!IsPostBack)
    {
        ddlSelectProgram.DataSource = new PEIATableAdapters.programTableAdapter().GetData();
        ddlSelectProgram.DataBind();

        ddlSelectSurveyType.DataSource = new PEIATableAdapters.ParticpantSurveyFormIDsTableAdapter().GetFormIDsByProgramID(ProgramID);
        ddlSelectSurveyType.DataBind();

        BindData();
    }

The call to BindData is correctly populating grdResults when the page loads:

 private void BindData()
{
    PEIATableAdapters.ParticipantSurveysTableAdapter adapter = new PEIATableAdapters.ParticipantSurveysTableAdapter();

    grdResults.DataSource = adapter.GetDataByFormID(FormID);        
    grdResults.DataBind();
}

However, when the page is posted back and DataBind is called again by clicking a Submit button, the value selected in ddlSelectProgram is not being passed in. The click event for the Submit button is merely calling Databind() and the event is firing.

Here are the Properties for ProgramID and FormID:

protected int ProgramID
{
    get
    {
        return Convert.ToInt32(ddlSelectProgram.SelectedValue);
    }
    set
    {
        ddlSelectProgram.SelectedValue = value.ToString();
    }
}

protected int FormID
{
    get
    {
        return Convert.ToInt32(ddlSelectSurveyType.SelectedValue);
    }
    set
    {
        ddlSelectSurveyType.SelectedValue = value.ToString();
    }
}

How do I tie the two controls together so that when ddl_SelectProgram is changed then ddlSelectSurveyType is changed?

Upvotes: 0

Views: 648

Answers (2)

Vince Fedorchak
Vince Fedorchak

Reputation: 1205

Add the property AutoPostBack="True" to ddlSelectProgram in the markup and add an event handler to the ddlSelectProgram's SelectedIndexChanged event in the code-behind. Bind ddlSelectSurveyType in that event.

Upvotes: 1

victor.t
victor.t

Reputation: 454

When page loads populate ddlSelectSurveyType only. Then use autoPostBack and catch event when value of ddlSelectSurveyType is changed. Then after you know ProgramID was selected populate your data.

Upvotes: 1

Related Questions