2013-03-03 63 views
0
<form> 
<asp:Repeater id="rptComments" runat="server"> 
    <HeaderTemplate> 
     <table class="detailstable FadeOutOnEdit"> 
      <tr> 
       <th style="width:200px;">Answers</th> 
      </tr> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <tr> 
      <th style="width:200px;"><asp:DropDownList ID="dropDownForChecklistAnswers" runat="server" /></th> 
     </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
     </table> 
     <asp:Button id="button" text="Submit" OnClick="Page_Load" runat="server" /> 
    </FooterTemplate> 
</asp:Repeater> 
</form> 

代碼背後:不會顯示下拉

List<Checklist_Record_Choice> CLRC = 
      (from choice in db.Checklist_Record_Choices 
      select choice).ToList(); 

     dropDownForChecklistAnswers.DataSource = CLRC; 

     DropDownList1.DataTextField = Text;//Text being the name of column2 in the table (which contains yes, no, n/a) 

     dropDownForChecklistAnswers.DataBind(); 

ERROR:dropDownForChecklistAnswers不會在當前上下文中存在??? 請指教


編輯; 感謝您的回覆。我有

public void OnReptDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    ClarkeDBDataContext db1 = new ClarkeDBDataContext(); 
    List<string> CLRC = 
    (from choice in db1.Checklist_Record_Choices 
    select choice.Text).ToList(); 

    DropDownList ddl = (DropDownList)e.Item.FindControl("dropDownForChecklistAnswers"); 
    ddl.DataSource = CLRC; 
} 

但DropDownList ddl回來了,因爲對象ref沒有設置爲對象的實例...爲什麼它是空的?

回答

1

您需要使用FindControl才能訪問屬於Repeater模板的控件。

訂閱的Repeater OnItemDataBound(設置屬性OnItemDataBound="OnReptDataBound"

然後在後面的代碼做的幫助,請看到我的編輯以下

void OnReptDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if(e.Item.ItemType == ListItemType.Item) 
    { 
    DropDownList ddl = (DropDownList)e.Item.FindControl("dropDownForChecklistAnswers"); 
    ddl.DataSource = .... 
+0

這就是偉大的感謝: – John 2013-03-03 22:08:03

+0

更新答案,請嘗試檢查[ItemType](http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.repeateritem.itemtype.aspx) – Blachshma 2013-03-03 22:12:23