James213
James213

Reputation: 977

How to obtain a selected value from dynamically built combobox

So i have a table that is built dynamically that adds rows for each machine in the database. Each row contains 4 columns and With 2 of the columns added to the table they contain a RadComboBox that gives the user the ability to select certain values that come from the database.

the method that builds each row for the table

        private void AddNewRow1(Machine Machine)
    {
        //start a new row
        TableCell site = new TableCell();
        TableCell name = new TableCell();
        TableCell type = new TableCell();
        TableCell model = new TableCell();
        TableRow tr = new TableRow(); 
        Literal breakline = new Literal();
        breakline.Text = "<br />";
        Literal breakline1 = new Literal();
        breakline1.Text = "<br />"; 

        //site name column
        site.RowSpan = 2;
        site.Controls.Add(AddSiteField(Machine));
        tr.Controls.Add(site);

        //machine name
        name.RowSpan = 2;
        name.Controls.Add(AddMachineField(Machine));
        tr.Controls.Add(name);

        //machine type name
        type.RowSpan = 2;
        type.Controls.Add(AddMachineTypeField(Machine));
        type.Controls.Add(breakline);
        type.Controls.Add(AddTypeComboBox());
        tr.Controls.Add(type);

        //machine model name
        model.RowSpan = 2;
        model.Controls.Add(AddMachineModelField(Machine));
        model.Controls.Add(breakline1);
        model.Controls.Add(AddModelComboBox());
        tr.Controls.Add(model);

        AssignPlaceHolder.Controls.Add(tr);
    }

The method in the model and type controls AddModelComboBox() or AddTypeComboBox() is below:

        private RadComboBox AddModelComboBox()
    {
        RadComboBox MachineModelCombo = new RadComboBox();
        machineModel = inputsService.GetMachineModelList(SiteID);
        foreach (MachineModel MachineModel in machineModel)
        {
            if (MachineModel.Name != "NULL")
            {                    
                MachineModelCombo.Items.Add(new RadComboBoxItem(MachineModel.Name, MachineModel.ID));
            }
        }

        MachineModelCombo.EnableLoadOnDemand = true;
        MachineModelCombo.EmptyMessage = "Select a Machine Model";
        return MachineModelCombo;
    }

The table works fine, and is built correctly. Where i am having my problem in the code below has to do with obtaining the value of these dynamically build comboboxes:

        protected void Update_Click(object sender, EventArgs e)
    {
      string MachineTypeID;
      string MachineModelID;
      machine = inputsService.GetMachineSiteDetails(SiteID);
      foreach (Machine Machine in machine)
      {
          try
          {
              RadComboBox machineTypeComboBox = new RadComboBox();
              RadComboBox machineModelComboBox = new RadComboBox();
              MachineTypeID = machineTypeComboBox.SelectedValue;
              MachineModelID = machineModelComboBox.SelectedValue;
              inputsService.UpdateMachineModels(Machine.ID, MachineTypeID);
              inputsService.UpdateMachineTypes(Machine.ID, MachineModelID);
          }
          catch (Exception ex)
          {
              {
                  logger.ErrorFormat(
                      "Update_Click exception occurred when attempting to update the database {0}", ex);
              }
          }
      }

My question is, how can i get the selected value from a dynamically built radcombobox?

Note:

              inputsService.UpdateMachineModels(Machine.ID, MachineTypeID);
              inputsService.UpdateMachineTypes(Machine.ID, MachineModelID);

these two lines are webservice calls to a DB api to make updates to the database depending on the item selected in the web page. The same goes for any call that starts with inputsService.*

any help or suggestions are greatly appreciated.

Thanks

Upvotes: 1

Views: 1024

Answers (1)

James213
James213

Reputation: 977

Okay i figured it out, which now after looking at it seemed pretty obvious...lol So i figured i would post it here in case anyone else needs an answer to this question.

What ended up being the solution to this problem was to give each of the comboboxes it own id. This was achieved with the following code below:

This is the now modified create a new combobox method:

    private RadComboBox AddModelComboBox(Machine machine)
    {
        RadComboBox MachineModelCombo = new RadComboBox();
        machineModel = inputsService.GetMachineModelList(SiteID);
        foreach (MachineModel MachineModel in machineModel)
        {
            if (MachineModel.Name != "NULL")
            {                    
                MachineModelCombo.Items.Add(new RadComboBoxItem(MachineModel.Name, MachineModel.ID));
            }
        }

        MachineModelCombo.ID = machine.ID.ToString() + "model";
        MachineModelCombo.EnableLoadOnDemand = true;
        MachineModelCombo.EmptyMessage = "Select a Machine Model";
        return MachineModelCombo;
    }

Machine goes into the DB and grabs an id for the machine. To make sure that the type and model methods don't obtain the same id a string was added to the machine id.

Now to obtain a selected value from a radComboBox i used to following code:

Note: this is still under development and does not include validations, and catches.

        protected void Update_Click(object sender, EventArgs e)
    {
      string MachineTypeID;
      string MachineModelID;
      machine = inputsService.GetMachineSiteDetails(SiteID);
      foreach (Machine Machine in machine)
      {
          try
          {
              string machinetypeid = Machine.ID.ToString() + "type";
              string machinemodelid = Machine.ID.ToString() + "model";
              Control type = MyExtensions.FindControlRecursive(this, machinetypeid);
              Control model = MyExtensions.FindControlRecursive(this, machinemodelid);
              RadComboBox machinetype = (RadComboBox) type;
              RadComboBox machinemodel = (RadComboBox) model;
              MachineTypeID = machinetype.SelectedValue;
              MachineModelID = machinemodel.SelectedValue;
              inputsService.UpdateMachineModels(Machine.ID, MachineModelID);
              inputsService.UpdateMachineTypes(Machine.ID, MachineTypeID);
          }
          catch (Exception ex)
          {
              {
                  logger.ErrorFormat(
                      "Update_Click exception occurred when attempting to update the database {0}", ex);
              }
          }
      }
      //clear out the old table and replace with the newly revized table.
      AssignPlaceHolder.Controls.Clear();  
      AddTableTitles();
      UpdateTableControls();
    }

Note: The controls in the second method are a method that obtains the dynamically built control by finding it's id/root in the html and gets the selected value through that.

Upvotes: 1

Related Questions