ASP.NET中继器不绑定

本文关键字:绑定 中继器 NET ASP | 更新日期: 2023-09-27 17:50:40

我有问题。

其中一个就是这个。

我试图将超链接列表绑定到中继器,我认为我的代码看起来都很好,但我的中继器遗憾地缺乏任何东西。它完全是空白的(除了头部,它不是绑定的)。

我看不出问题在哪里,所以我希望你们能给我指出来。

代码:

标记

<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方法,因为面板的其余部分加载良好(搜索栏和标题等),但没有我的链接在那里。

ASP.NET中继器不绑定

After

rptComputers.DataSource = Data;

添加
rptComputers.DataBind();

您可能在aspx文件的Page header中缺少AutoEventWireup=true

如果没有,请尝试在Page_Load事件中绑定中继器,而不是在Page_Init事件中绑定。