不会显示下拉

时间:2013-03-03 21:14:26

标签: asp.net drop-down-menu

    <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 ??? 请指教


EDIT; 谢谢你的答复。我有

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返回,因为对象引用未设置为对象的实例...为什么它为null ??

1 个答案:

答案 0 :(得分: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 = ....
相关问题