中继器使用哪个事件来查找中继器内的控件

发布于 2024-11-29 08:19:48 字数 1794 浏览 1 评论 0原文

我使用 asp.net 和 c#4。

我在 HyperLink 控件内有一个中继器。

我需要找到 HyperLink 控件,并在将其呈现在页面上之前使用一些逻辑更改其某些属性。

在这里发布我的代码后,我得到了该控件的空值,因此我无法获取它。

知道我做错了什么吗?感谢您抽出时间来讨论这个问题。

<asp:Repeater ID="RepeaterEditorsChoice" runat="server" DataSourceID="ObjectDataSourceEditorsChoice"
    OnItemCreated="RepeaterEditorsChoice_ItemCreated" OnItemDataBound="RepeaterEditorsChoice_ItemDataBound">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:HyperLink ID="uxLink" runat="server"></asp:HyperLink>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

背后的代码:

protected void RepeaterEditorsChoice_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HyperLink myLink = (HyperLink)((Repeater)sender).FindControl("uxLInk"); // ERROR: it is null
            dynamic o = e.Item.DataItem;
            if (o.TypeContent == "AR")
            {

                myLink.Text = @"'<%# Eval(\""Title\"") %>'";
                myLink.NavigateUrl = @"'<%# GetRouteUrl(""ArticleDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
            }
            if (o.TypeContent == "BP")
            {
                myLink.Text = @"'<%# Eval(\""Title\"") %>'";
                myLink.NavigateUrl = @"'<%# GetRouteUrl(""BlogPostDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
            }
        }
    }

I use asp.net and c#4.

I have a repeater within inside a HyperLink Control.

I need to Find the HyperLink Control and change some of its properties with some logic before rendering it on the page.

With my code here posted I get a null value for the control so I'm not able to get it.

Any idea what I'm doing wrong? Thanks for your time on this.

<asp:Repeater ID="RepeaterEditorsChoice" runat="server" DataSourceID="ObjectDataSourceEditorsChoice"
    OnItemCreated="RepeaterEditorsChoice_ItemCreated" OnItemDataBound="RepeaterEditorsChoice_ItemDataBound">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:HyperLink ID="uxLink" runat="server"></asp:HyperLink>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

CODE BEHIND:

protected void RepeaterEditorsChoice_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HyperLink myLink = (HyperLink)((Repeater)sender).FindControl("uxLInk"); // ERROR: it is null
            dynamic o = e.Item.DataItem;
            if (o.TypeContent == "AR")
            {

                myLink.Text = @"'<%# Eval(\""Title\"") %>'";
                myLink.NavigateUrl = @"'<%# GetRouteUrl(""ArticleDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
            }
            if (o.TypeContent == "BP")
            {
                myLink.Text = @"'<%# Eval(\""Title\"") %>'";
                myLink.NavigateUrl = @"'<%# GetRouteUrl(""BlogPostDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-12-06 08:19:48

在项目数据绑定上

,只需 (YourClass)e.Item.FindControl("its name");

On item data bound

then just (YourClass)e.Item.FindControl("its name");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文