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

错误: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 又回来了,因为对象引用未设置为对象的实例......为什么它是空的?

4

1 回答 1

1

您需要使用FindControl访问作为中继器模板一部分的控件。

订阅OnItemDataBoundRepeater的(设置属性OnItemDataBound="OnReptDataBound"

然后在你后面的代码中执行以下操作

void OnReptDataBound(object sender, RepeaterItemEventArgs e)
{
   if(e.Item.ItemType == ListItemType.Item) 
   {
     DropDownList ddl = (DropDownList )e.Item.FindControl("dropDownForChecklistAnswers");
     ddl.DataSource = ....
于 2013-03-03T21:27:16.387 回答