无法看到中继器

时间:2012-10-31 09:37:36

标签: c# asp.net repeater

我在转发器内加载Dropdown但我无法看到转发器。为什么我无法看到它?

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;
        }

    }

2 个答案:

答案 0 :(得分: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
  • 它的DataBind为空或空
  • 你根本没有数据库
  • ....(没有代码,很难进一步帮助)

修改请不要发表评论,但请使用重要信息修改您的问题。

  

断点不会进入转发器项数据绑定范围内 -   新手25分钟前

然后你没有DataSource或{{1}}为空或空。您应该向我们展示分配数据源的代码并调用数据绑定。

答案 1 :(得分: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上,如果你在ASPX端没有"AutoEventWireUp = true",你可能想要为转发器绑定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();
    }
}