Control not showing in CodeBehind?

I have my main page:

<%@ 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>

      

There I am:

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

      

This way, the links appear in the code.

Now if I move them to the repeater footer:

...

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

      

Then they can no longer be used in the code behind.

I really don't get it.

Why isn't it working?

+3


source to share


3 answers


RepeaterItem

has another NamingContainer

. You can only access controls that are on top of the page, as these controls are created automatically in the partial code class. You must use FindControl

to get the link of the control in RepeaterItem

.

In this case, you can use an event 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");
    }
}   

      

+2


source


Since the controls are no longer on the main form, they are no longer properties Page

. Instead, they can be accessed via Repeater

.



See this question for an example on accessing controls via Repeater

.

0


source


When you put objects in Repeaters, Grids, etc., you need to do a lot more processing in the backend to find them. Basically, when a repeater is connected, you can go through and search for an object and then access its properties.

For example, here is a link to some code for a relayer that walks through various item controls when an item is anchored

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

      

Personally, I've never tried to get to the controls in the repeater header or footer, but I'm pretty sure it would be something similar

0


source







All Articles