0

我正在中继器中加载下拉菜单,但我看不到中继器。为什么我看不到它?

aspx:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">    
    <ItemTemplate> 
       <table>
           <tr>
             <td>
               <asp:DropDownList ID="DropDownList1" runat="server">                     
                  <asp:ListItem>Select</asp:ListItem> <asp:ListItem>Left</asp:ListItem> 
                  <asp:ListItem>Right</asp:ListItem> 
                  <asp:ListItem>SubString</asp:ListItem>
               </asp:DropDownList> 
              <asp:DropDownList ID="DropDownList6" runat="server"> </asp:DropDownList> 

代码隐藏:

 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

        DataTable dt = new DataTable();
        dt = Common.LoadExample();
        DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList6") ;
        ddl.DataSource = dt;
        ddl.DataTextField = "Name";
        ddl.DataValueField = "Name";
        ddl.DataBind();

}

// 用于加载的数据表

   public static DataTable LoadExample()
    {
        DBAccess objDBAccess = new DBAccess();
        DataTable dt = new DataTable();
        try
        {
            objDBAccess.AddParameter("@Name", SqlDbType.VarChar);
            dt = objDBAccess.ExecuteDataTable("usp_test");
            return dt;
        }
        catch 
        {

            return null;
        }

    }
4

2 回答 2

0

我认为原因是您没有检查ItemType,因此您遇到异常,因为标题不包含DropDownList但仅包含项目:

protected void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    // This event is raised for the header, the footer, separators, and items.
    // Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        // ...
    }
}

当然,中继器不可见还有其他原因。

  • Repeater或者它的容器控件之一是Visible="false"
  • DataSource是空的或空的
  • 你根本没有数据绑定
  • ....(没有代码很难进一步帮助)

编辑请不要发表评论,而是用重要信息编辑您的问题。

断点不在中继器项目数据绑定内 – 新手 25 分钟前

然后你没有DataBind它或者它DataSource是空的或空的。您应该向我们展示您分配数据源并调用数据绑定的代码。

于 2012-10-31T09:47:21.140 回答
0

这只是一个示例,让您了解事情应该如何:

在 ASPX 页面上:

<asp:Repeater runat="server" ID="rptrTest">
    <ItemTemplate>
        ID: <%#DataBinder.Eval(Container.DataItem, "ID")%>
        <br />
        Name: <%#DataBinder.Eval(Container.DataItem, "Name")%>
        <br />
        Select: <asp:DropDownList runat="server" ID="ddlSelect" />
        <br /><br />
    </ItemTemplate>
</asp:Repeater>

在 Page_Init 上,如果您没有"AutoEventWireUp = true"ASPX 端,您可能希望为转发器绑定 ItemDataBound 事件:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    rptrTest.ItemDataBound +=
        new RepeaterItemEventHandler(rptrTest_ItemDataBound);
}

在 Page_Load 上:

protected void Page_Load(object sender, EventArgs e)
{
    // This is an example, but you should get this data from database
    List<File> myObjects = new List<File>(new[]
    {
        new File { ID = 1, Name = "Foo", AListOfStrings = stringList },
        new File { ID = 2, Name = "Bar", AListOfStrings = stringList },
        new File { ID = 3, Name = "Baz", AListOfStrings = stringList },
        new File { ID = 4, Name = "Quux", AListOfStrings = stringList }
    });

    rptrTest.DataSource = myObjects;
    rptrTest.DataBind();
}

在中继器的 ItemDatabound 事件上:

void rptrTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    {
        // Find the DropDownList in the repeater's ItemTemplate
        // so we can manipulate it.
        DropDownList ddlSelect =
            e.Item.FindControl("ddlSelect") as DropDownList;
        if (ddlSelect == null) return;

        DataTable dt = new DataTable();
        dt = Common.LoadExample();

        ddlSelect .DataSource = dt;
        ddlSelect .DataTextField = "Name";
        ddlSelect .DataValueField = "Name";
        ddlSelect .DataBind();
    }
}
于 2012-10-31T09:55:26.830 回答