1

I have an edit page where I set the selected index of a radcombobox (rcb_ParentCompany) based on a value returned from the database. However on postback the text in the combobox keeps changing to the top item in the dataset. Any ideas why?

protected void Page_Load(object sender, EventArgs e)
    {
         if (IsPostBack)
         {
             BindOperatingNameComboBox(rcb_OperatingName);
             BindParentCompanyComboBox(rcb_ParentCompany);
         }
    }



protected void btn_Edit_Command(object sender, CommandEventArgs e)
    {
        Client ClientToEdit = ClientController.ViewClient(int.Parse(e.CommandArgument.ToString()));

        //Populate Client fields
        txt_ClientName.Text = ClientToEdit.ClientName;
        rcb_OperatingName.Text = ClientToEdit.OperatingName;
        int ParentCompanyIndex = rcb_ParentCompany.FindItemIndexByValue(ClientToEdit.ParentCompanyID.ToString());
        rcb_ParentCompany.SelectedIndex = ParentCompanyIndex;
        txt_Address1.Text = ClientToEdit.Address1;
        txt_Address2.Text = ClientToEdit.Address2;
        txt_Country.Text = ClientToEdit.Country;
        txt_Region.Text = ClientToEdit.Region;
        txt_City.Text = ClientToEdit.City;
        txt_PostalCode.Text = ClientToEdit.PostalCode;
        txt_ClientNote.Text = ClientToEdit.ClientNote;

        tbl_EditServices.Controls.Clear();
        PopulateEditClientPanel(ClientToEdit);

        btn_SaveChanges.CommandArgument = e.CommandArgument.ToString();
        btn_Cancel.CommandArgument = e.CommandArgument.ToString();
    }



protected void BindParentCompanyComboBox(RadComboBox ComboBox)
    {
        DataTable OperatingNames = ClientController.GetExistingClientAndOperatingNames("");
        ComboBox.DataTextField = "ClientName";
        ComboBox.DataValueField = "ClientID";
        ComboBox.DataSource = OperatingNames;
        ComboBox.DataBind();
    }



protected void rcb_ParentCompany_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        BindParentCompanyComboBox((sender as RadComboBox));
    }
cpiasecki
  • 47
  • 1
  • 8

1 Answers1

1

Any ideas why?

Yes, because you are doing if(IsPostBack) as opposed to if(!IsPostBack)

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • But the combobox needs to be binded on every postback. Else the combobobox will be empty. – cpiasecki Aug 01 '12 at 18:16
  • No, why? It should maintain the state as long as you have ViewState enabled. Is it not the case here? – Icarus Aug 01 '12 at 18:33
  • I do have viewstate enabled, but if i take away the binding inside page load i get the error when i move focus outside of the combobox "There is no assigned data source. Unable to complete callback request" – cpiasecki Aug 01 '12 at 18:45
  • I managed to fix the problem by setting EnableLoadOnDemand = true after I setting finding the index by value and setting it since having it true beforehand would cause null returns from the finding item methods. – cpiasecki Aug 01 '12 at 19:03