gpt4 book ai didi

c# - 控件没有出现在 CodeBehind 中?

转载 作者:行者123 更新时间:2023-11-30 17:07:12 25 4
gpt4 key购买 nike

我有我的主页:

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

然后它们就不能再在代码隐藏中使用了。

这个我真的很不明白。

为什么它不起作用?

最佳答案

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

在这种情况下,您可以使用 RepeaterItemDataBound事件:

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

关于c# - 控件没有出现在 CodeBehind 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14755771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com