ASP.NET Repeater没有绑定

时间:2015-05-25 22:58:34

标签: c# asp.net repeater

我有问题。

其中一个就是这个。

我试图将超链接列表绑定到转发器上,我认为我的代码看起来很好,但我的转发器遗憾地缺少任何东西。它完全是空白的(除了标题,不是数据绑定)。

我无法看到问题出在哪里,所以我希望你们能指出我的问题。

代码:

标记

<asp:Panel ID="pnlNavMenu"  class="navigation" runat="server" Visible="true">
<div class="search-textbox"><div>

                   <asp:ImageButton ID="btnSearch" class="Search-Icon"
                        BackColor="White"  runat="server" OnClick="btnSearch_Click" 
                        ImageUrl="~/images/Mobile/mobile-search-icon.png" />

                   <asp:TextBox ID="txtSearch" runat="server" CssClass="Search" onblur="if(this.value == '') { this.value='Enter keyword or product code'; isSet=true; }"
                        onmouseover="if(this.value == 'Enter keyword or product code') { this.value='';isSet = true; }"
                        onmouseout="if(this.value == '' && !isSet) { this.value='Enter keyword or product code'; isSet=>false; }"
                        MaxLength="255" Text="Enter keyword or product code" ontextchanged="btnSearch_Click"/>

                   <asp:ImageButton ID="btnClear" class="Search-Cancel" BackColor="White" runat="server" OnClick="btnClear_Click" ImageUrl="~/images/Mobile/mobile-search-cancel.png" />

                    </div>
                    </div>

                   <asp:Panel ID="pnlComputers" runat="server" CssClass="nav-item" Visible="true">

                    <asp:Label id="lblComp" Text="Computers" runat="server" cssclass="Menu-Panel-Header"></asp:Label>

            <asp:Repeater ID="rptComputers" runat="server">

            <ItemTemplate><asp:HyperLink ID="hlCompCategories" runat="server" CssClass="nav-sub-item"><%#Eval("XW_WEBCATNAME") %></asp:HyperLink></ItemTemplate>

            </asp:Repeater> 

           </asp:Panel

            <asp:CollapsiblePanelExtender ID="cpe1" runat="Server" TargetControlID="pnlComputers" CollapsedSize="64" ExpandedSize="192" Collapsed="True" ExpandControlID="lblComp" CollapseControlID="lblComp" AutoCollapse="false" AutoExpand="False" ScrollContents="True" ExpandDirection="Vertical" />

             </asp:Panel>

C#

protected void Page_Init(object sender, EventArgs e)
{
    if (Session["Customer"] is GPCUser)
    {
        hlLogInOut.Text = "Log Out";
        hlLogInOut.NavigateUrl = "log-in.aspx?logout=1";
        hlRegDetails.Text = "My Details";
        hlRegDetails.NavigateUrl = "/update-details.aspx";
    }
    else
    {
        hlLogInOut.Text = "Log in";
        hlLogInOut.NavigateUrl = "/log-in.aspx";
        hlRegDetails.Text = "Register";
        hlRegDetails.NavigateUrl = "/create-account.aspx";
    }

    BindCategories();
}


private void BindCategories()
{



    if (!IsPostBack)
    {
        try
        {
            SqlConnection connect = new SqlConnection();

            DataTable Data = new DataTable();


            connect.ConnectionString = "SERVER = SERVER-SQL01; Trusted_Connection=yes; DATABASE=PCSQL";
            connect.Open();

            string query = null;

            query = "SELECT * from dbo.STOCK_GROUPS WHERE XW_MAINGROUP = '1' ORDER BY XW_WEBCATNAME ASC";

            SqlDataAdapter command = new SqlDataAdapter(query, connect);
            command.Fill(Data);
            connect.Close();

            rptComputers.DataSource = Data;



        }
        catch (SqlException sqlEX)
        {
            sqlEX.ToString();
        }
    }

}

protected void rptComputers_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HyperLink hlCompCategories = (HyperLink)e.Item.FindControl("hlCompCategories");

        DataRowView dr = (DataRowView)e.Item.DataItem;

        hlCompCategories.NavigateUrl = Page.ResolveUrl("~/" + "Computers" + "/" + dr["XW_URL"] + "/index.aspx");

        if ((!object.ReferenceEquals(dr["xw_webcatname"], System.DBNull.Value)))
        {
            hlCompCategories.Text = (dr["xw_webcatname"]).ToString();
            hlCompCategories.ToolTip = (dr["xw_webcatname"]).ToString();
        }
        else
        {
            hlCompCategories.Text = dr["groupname"].ToString();
            hlCompCategories.ToolTip = dr["xw_webcatname"].ToString();
        }

    }

}

我很确定问题出在ItemDataBound方法中,因为面板的其余部分加载正常(搜索栏和标题等),但我的链接都没有。

2 个答案:

答案 0 :(得分:2)

rptComputers.DataSource = Data;

添加

rptComputers.DataBind();

否则它不会绑定。

答案 1 :(得分:2)

您可能在aspx文件的页眉中缺少AutoEventWireup=true

如果不是,请尝试绑定Page_Load事件中的转发器,而不是绑定Page_Init事件。