3

我有我的母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="KezberProjectManager.master.cs" Inherits="KezberProjectManager.KezberProjectManager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <!-- Le styles -->
    <link href="assets/css/bootstrap.css" rel="stylesheet"/>
    <link href="assets/css/bootstrap-responsive.css" rel="stylesheet"/>
    <link href="assets/css/kezblu.styles.css" rel="stylesheet"/>

    <style type="text/css">

    </style>
    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
    <script type="text/javascript" src="assets/js/kezcommon.js"></script>
    <script type="text/javascript">

        $(document).ready
    (
        function () {

            createAutoClosingAlert('.success_alert', 6000);
        }
    );

        function createAutoClosingAlert(selector, delay) {
            var alert = $(selector).alert();
            window.setTimeout(function () { $(alert).slideUp() }, delay);
        }

</script>  


    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
<asp:Repeater runat="server" id="MenuRepeater">
  <headertemplate>
       <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="#">KezBlu</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
  </headertemplate>
  <itemtemplate>
     <%# Eval("Content") %>
  </itemtemplate>
  <footertemplate>
             </ul>

          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>
  </footertemplate>
 </asp:Repeater>


    <div id="wrap">
       <div id="content">
           <div id="alerts">
               <div class="bs-docs-example">
                        <div id="auth">
                 <asp:HyperLink id="HyperLink1" runat="server">HyperLink</asp:HyperLink>
             </br>
                 <asp:HyperLink id="HyperLink2" runat="server">HyperLink</asp:HyperLink>
             </div>
              <div runat="server"  id="success_alert" class="success_alert alert alert-success fade in">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                   <div runat="server" id="success_alert_text">
                </div>
              </div>
             </div>
              <div class="bs-docs-example">
              <div runat="server" id="error_alert" class="error_alert alert alert-error fade in">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                <div runat="server" id="error_alert_text">
                </div>
              </div>
             </div>
            </div>
       <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

       </asp:ContentPlaceHolder>
       </div>


    </div>
    </form>
      <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
</body>
</html>

在那里我有:

     <div id="auth">
         <asp:HyperLink id="HyperLink1" runat="server">HyperLink</asp:HyperLink>
     </br>
         <asp:HyperLink id="HyperLink2" runat="server">HyperLink</asp:HyperLink>
     </div>

像这样,链接在后面的代码中显示得很好。

现在,如果我将它们移到中继器的页脚中:

...

          <div class="nav-collapse collapse">
            <ul class="nav">
  </headertemplate>
  <itemtemplate>
     <%# Eval("Content") %>
  </itemtemplate>
  <footertemplate>
             </ul>
             <div id="auth">
                 <asp:HyperLink id="HyperLink1" runat="server">HyperLink</asp:HyperLink>
             </br>
                 <asp:HyperLink id="HyperLink2" runat="server">HyperLink</asp:HyperLink>
             </div>

          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>
  </footertemplate>
 </asp:Repeater>

然后它们不再可以在后面的代码中使用。

我真的真的不明白这一点。

为什么它不起作用?

4

3 回答 3

2

RepeaterItem有不一样的 NamingContainer。您只能直接访问页面顶部的控件,因为这些控件是在部分代码隐藏类中自动创建的。你必须使用FindControl来获取一个控件的引用RepeaterItem

在这种情况下,您可以使用Repeater'ItemDataBound事件:

protected void Repater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
    // This event is raised for the header, the footer, separators, and items.
    if (e.Item.ItemType == ListItemType.Footer)
    {
        HyperLink hl = (HyperLink)e.Item.FindControl("HyperLink1");
    }
}   
于 2013-02-07T16:29:56.490 回答
0

因为控件不再位于主窗体上,它们不再是Page. 它们可以改为通过Repeater.

有关通过. _Repeater

于 2013-02-07T16:20:45.183 回答
0

当您将对象放入中继器、网格等时,您必须在后端进行更多处理才能找到它们。基本上,当中继器绑定时,您可以遍历并搜索对象,然后访问其属性。

例如,这里是一些代码的链接,用于在绑定项目时查找各种项目控件的中继器

protected void rptTaskListOnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var lnkEdit = e.Item.FindControl("lnkEdit") as HyperLink;
            var lnkDelete = e.Item.FindControl("lnkDelete") as LinkButton;

            var pnlAdminControls = e.Item.FindControl("pnlAdmin") as Panel;

            var t = (Task)e.Item.DataItem;

            if (IsEditable && lnkDelete != null && lnkEdit != null && pnlAdminControls != null)
            {
                pnlAdminControls.Visible = true;
                lnkDelete.CommandArgument = t.TaskId.ToString();
                lnkDelete.Enabled = lnkDelete.Visible = lnkEdit.Enabled = lnkEdit.Visible = true;

                lnkEdit.NavigateUrl = EditUrl(string.Empty, string.Empty, "Edit", "tid=" + t.TaskId);

                ClientAPI.AddButtonConfirm(lnkDelete, Localization.GetString("ConfirmDelete", LocalResourceFile));
            }
            else
            {
                pnlAdminControls.Visible = false;
            }
        }
    }

就我个人而言,我从未尝试过访问转发器的页眉或页脚中的控件,但我相信它会是类似的

于 2013-02-07T16:21:55.537 回答