0

I have implented a drop down list and I want to populate the data table according to the selected value in the drop down list. But currently I am unable to display the selectedIndexChanged. Please assist.

  protected void ddlitem_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable result = new DataTable();

            result.Columns.Add("Supplier");
            result.Columns.Add("Country");
            result.Columns.Add("Partnership Duration");
            result.Columns.Add("Partnership Type");

            int intMemberIndex = 0;
            try
            {
                foreach (SPListItem objtestListItem in objtestList.Items)
                {
                    string SupplierName = objtestListItem["Supplier"].ToString();
                    string Country = objtestListItem["Country"].ToString();
                    string PD = objtestListItem["Partnership Duration"].ToString();
                    string PT = objtestListItem["Partnership Type"].ToString();
                    intMemberIndex = objtestListItem.ID;

                    for (int i = 0; i < ddlitem.Items.Count; i++)
                    {
                        if (ddlitem.Items[i].Text.Equals(SupplierName))
                        {
                            result.Rows.Add(SupplierName, Country, PD, PT);


                        }
                    }

                } this.resultGrid.DataSource = result;
                this.resultGrid.Visible = true;
                this.resultGrid.DataBind();
            }
            catch (Exception ex)
            {

                lblTxt.Text = ex.Message;
            }


        }



    }
Dwayne Johnson
  • 69
  • 1
  • 6
  • 18

1 Answers1

0

The markup for the dropdown should be like bellow

<asp:DropDownList ID="ddlitem" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlitem_SelectedIndexChanged">
</asp:DropDownList>

this will call ddlitem_SelectedIndexChanged and bind data to resultGrid.

you may need following code inside your page load method to execute something ONLY on first load

if(!Page.IsPostBack)
{
   //Control Initialization
   //Databinding
}
Damith
  • 62,401
  • 13
  • 102
  • 153