该名称在当前上下文(ItemTemplate)中不存在

本文关键字:ItemTemplate 不存在 上下文 | 更新日期: 2023-09-27 18:03:37

我在ASP上的ItemTemplate中有一些控件。网络页面。但是,我无法在代码隐藏文件中访问这些控件。我只是得到这个错误:

名称在当前上下文中不存在

如果我添加的控件不在我的ItemTemplate中,它就会工作,所以我假设我需要做一些其他的事情来访问这些ItemTemplate控件。那么我怎样才能在我的aspx.cs文件中访问这些控件呢?

代码:

                <ItemTemplate>
                    <div class="headline">
                        <h2>
                            <asp:Label ID="lblGameTitle" runat="server" Text='<%#Eval("GameTitle") %>'></asp:Label></h2>
                        <div class="blog">
                            <div class="blog-post-tags" style="padding-top: 5px">
                                <ul class="list-unstyled list-inline blog-info">
                                    <li><i class="fa fa-globe"></i> Produsent:
                                    <asp:Label ID="lblGamePublisher" runat="server" Text='<%#Eval("GamePublisher") %>'></asp:Label></li>
                                    <li><i class="fa fa-calendar"></i> Utgivelsesdato:
                                    <asp:Label ID="lblGameReleaseDate" runat="server" Text='<%#Eval("GamePublishedDate") %>'></asp:Label></li>
                                    <li><i class="fa fa-tag"></i> Sjanger:
                                    <asp:Label ID="lblGameGenre" runat="server" Text='<%#Eval("GenreName") %>'></asp:Label></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-5">
                            <asp:Image ID="imgGameImage" runat="server" ImageUrl='<%#Eval("GameImagePath") %>' Height="100%" Width="100%" />
                        </div>
                        <div class="col-md-7">
                            <div class="row">
                                <div class="tag-box tag-box-v6">
                                    <asp:Button ID="btnRentGame" class="btn-u btn-u-lg btn-u-orange" runat="server" Text="Lei spill" /></p>
                                     <div id="errorNoAccount" runat="server" class="alert alert-info fade in">
                                         <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                         <h4>Ops!</h4>
                                         <p>For å leie spill, må du registrere en bruker og ha et aktivt abonnement.</p>
                                         <p>
                                             <a class="btn-u btn-u-xs btn-u-default" href="#">Bli medlem</a>
                                         </p>
                                    </div>
                                </div>
                                <div class="tag-box tag-box-v6">
                                    <asp:Label ID="lblGameDescription" runat="server" Text='<%#Eval("GameDescription") %>'></asp:Label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <br />
                        <div class="col-md-12">
                            <div class="panel panel-u">
                                <div class="panel-heading">
                                    <h3 class="panel-title"><i class="fa fa-video-camera"></i>Trailer</h3>
                                </div>
                                <div class="panel-body" style="height: 100%">
                                    <div class="responsive-video">
                                        <iframe style="border: none; width: 100%; height: 100%" src='<%# "//www.youtube.com/embed/" + Eval("GameTrailer") + "?rel=0&showinfo=0&iv_load_policy=3" %>' allowfullscreen></iframe>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </ItemTemplate>

该名称在当前上下文(ItemTemplate)中不存在

如果你有一个网格视图。然后像这样考虑标记:

<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblYourLabel" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

你可以像这样在代码后面找到标签:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var lblYourLabel = ((Label) e.Row.FindControl("lblYourLabel"));
        lblYourLabel.Text = "SomeValue";
    }
}