2011-10-25 32 views
1

因此,我有一個動態構建的表,它爲數據庫中的每臺計算機添加行。每行包含4列,並且向表中添加了2列,它們包含一個RadComboBox,使用戶能夠選擇來自數據庫的某些值。如何從動態構建的組合框中獲取選定的值

該構建每一行表

 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); 
    } 

的方法,在模型和類型控制AddModelComboBox()或AddTypeComboBox()該方法是如下:

 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; 
    } 

表工作正常,並且被正確地建造。 如果我有下面的代碼我的問題已經做得到這些動態地構建組合框的值:

 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); 
       } 
      } 
     } 

我的問題是,我怎麼能得到一個動態構建radcombobox控件選擇的值?

注:

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

這兩條線是Web服務調用一個DB API取決於在網頁中選擇的項目進行更新到數據庫中。任何以inputsService開頭的電話都是如此。*

任何幫助或建議都非常感謝。

感謝

+0

您是否循環遍歷在更新事件中創建的錶行? –

+0

它通過對機器對象中找到的每個機器進行循環,這是通過調用數據庫獲得的。 – James213

回答

1

好吧,我想通了,它看着它後,現在似乎很明顯...笑所以我想我會在這裏發佈的情況下,任何人都需要一個回答這個問題。

最終解決這個問題的最終方法是給每個組合框自己的id。 這與下面的下面的代碼實現:

這是現在修改創建新的ComboBox方法:

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; 
    } 

機進入數據庫,並抓住了機器的ID。爲了確保類型和模型方法不會獲得相同的id,將一個字符串添加到機器標識中。

現在從radcombobox控件我用於下面的代碼獲得一選定值:

注意:這是仍處於開發階段,並且不包括驗證,和抓住。

 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(); 
    } 

注:在第二種方法中的控制是由尋找它的id /根的HTML並通過獲取所選值獲得動態構建控制的方法。

相關問題