2011-05-19 58 views
2

我已經DROPDOWNLIST我想要得到的值Repeater控件的內部,我得到「未將對象引用設置到對象的實例」中選擇下拉列表值。我不知道什麼嘗試。由於獲取中繼控制

ASPX CODE:

<asp:Repeater ID="GeneralRepeater" runat="server" 
      OnItemDataBound="GeneralRepeater_OnItemDataBound" 
       onitemcommand="GeneralRepeater_ItemCommand"> 
      <HeaderTemplate></HeaderTemplate> 
      <ItemTemplate> 
      <tr> 
      <td class="style2"> 
      </td> 
       <td class="style2"> 
       <asp:DropDownList ID="GeneralDDL" AppendDataBoundItems="true" DataTextField="DiagnosisCode" 
        DataValueField="DiagnosisCode" runat="server" AutoPostBack="True" /> 
       </td> 

      </tr> 
      </ItemTemplate> 
      <FooterTemplate></FooterTemplate> 
      </asp:Repeater> 
     </asp:Panel> 

後面的代碼:

protected void GeneralRepeater_OnItemDataBound(object sender, 
               RepeaterItemEventArgs e) 
    { 
     if (e.Item.ItemType == ListItemType.Item || 
      e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL"); 
      Diagnosis oDiagnosis = new Diagnosis(); 
      DataView dv = new DataView(oDiagnosis.GetDiagnosis()); 
      myDDL.DataSource = dv; 
      myDDL.DataTextField = "DiagnosisCode"; 
      myDDL.DataValueField = "DiagnosisCode"; 
      myDDL.DataBind(); 

     } 
    } 

protected void cmdSave_Click(object sender, EventArgs e) 
{ 
    string ProductSelected; 
    string FeatureSelected; 
    string SectionSelected; 


    foreach(RepeaterItem dataItem in GeneralRepeater.Items) 
    { 
     ProductSelected = ((DropDownList)GeneralRepeater.FindControl("GeneralDDL")).SelectedItem.Text; //error Object reference not set to an instance of an object. 
    }     
} 

我有上保存選定值問題..

回答

9

我發現在代碼您是通過generalrepeater項目循環。您正在訪問的repeater項目的DataItem的。我嘗試了這種代碼在這裏,理想的代碼應該說的

foreach(RepeaterItem dataItem in GeneralRepeater.Items)   
{    
    ProductSelected = ((DropDownList)dataItem.FindControl("GeneralDDL")).SelectedItem.Text; //No error 
} 

代替

foreach(RepeaterItem dataItem in GeneralRepeater.Items) 
{ 
    ProductSelected = ((DropDownList)GeneralRepeater.FindControl("GeneralDDL")).SelectedItem.Text; //error Object reference not set to an instance of an object. 
} 
+1

我沒有試過,但我認爲中怡是正確的。雖然「DataItem的」可能是有點用詞不當。 – 2011-05-19 03:27:00