【问题标题】:ASP.NET Repeater not bindingASP.NET 中继器未绑定
【发布时间】:2015-05-25 22:58:33
【问题描述】:

我有问题。

其中一个就是这个。

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

我看不出问题出在哪里,所以希望大家能指出来。

代码:

标记

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

【问题讨论】:

  • 您使用了调试器吗?你在那里看到什么? (你进入rptComputers_ItemDataBound了吗?)
  • Visual Studio 调试器在此网站上不起作用。我的老板(大约十年前自己编写了网站,从那以后只进行了零星更新)告诉我这是由于每个页面都通过 API 与 SQL 和 MYOB 以及其他一些零碎链接的方式,调试器无法引用他们正确,但我不知道这是否属实。我只需要刷新 Chrome 以检查是否有效。无论如何它是针对移动页面的,所以我正在检查我的 S5。
  • 与论坛网站不同,我们不使用“谢谢”、“任何帮助表示赞赏”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
  • 我的坏习惯是在公司内部发送内部电子邮件,如果您忘记自己的位置,大多数高级员工都会让自己陷入括约肌紧缩。

标签: c# asp.net repeater


【解决方案1】:

之后

rptComputers.DataSource = Data;

添加

rptComputers.DataBind();

否则不会绑定。

【讨论】:

  • 我不敢相信我错过了。真的在这里赚到我的工资。感谢您指出这一点,但我仍然遇到中继器中没有项目的问题?
  • 直接对数据库运行查询。确保结果不为空。
  • 如果我在 SQL 管理器中运行,查询将返回正确的结果。
  • @ChrisHinton 然后确保到达rptComputers.DataSource = Data; 并且Data 包含>0 行。
【解决方案2】:

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

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

【讨论】:

  • 谢谢,但不幸的是它没有用。我有 AutoEventWireup=true 并将 bindCategoires() 移动到页面加载事件,但遗憾的是没有任何改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多