Search code examples
asp.netcode-behindobject-reference

Intellisense not showing up for some controls in code behind


i'm kind of an asp.net beginner and i'm trying just to set the visible attribute dynamically but i can't access "TestLinkBox" nor the "TestLink" id in the code behind. Ive tried to rebuild the solution, to delete the designer.cs file and let it be recreated and tried a few solutions I found on stackOverflow as you can see in the code behind but i always get the "Object reference not set to an instance of an object." error. What am I doing wrong?

PART OF THE ASP.NET CODE

<%@ Page Title="" Language="C#" MasterPageFile="~/Public/Main.Master"
AutoEventWireup="true" CodeBehind="SpecifikacijaDetails.aspx.cs" Inherits="web.Public.SpecifikacijaDetails"
%>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <mc:VijestiTraka ID="vijestitraka" runat="server"></mc:VijestiTraka>
        <div class="middle_content_box">
            <asp:ListView ID="ListView1" runat="server" DataKeyNames="SpecifikacijaID"
            DataSourceID="SqlDataSource1">
                <AlternatingItemTemplate>
                    <div class="details_title_box">
                        <p class="title"><strong><%# Eval("Brand") %></strong> 
                            <%# Eval( "Model") %>
                        </p>
                    </div>
                    <div class="left_column">
                        <div class="pregled_modela_slika margb30">
                            <img src='<%# Eval("SlikaMobitela") %>' />
                        </div>
                        <div id="TestLinkBox" class="testLink" runat="server" visible="false">
                            <asp:HyperLink runat="server" ID="TestLink" NavigateUrl='<%#"~/Public/TestSpecifikacije.aspx?SpecifikacijaID=" + Eval("SpecifikacijaID")%>'
                            Text="Pogledajte kompletan test"></asp:HyperLink>
                        </div>

CODE BEHIND

using (MobBL temp = new MobBL())
{
    if (temp.ProvjeriImalTest(Int32.Parse(Request.QueryString["SpecifikacijaID"])) > 0)
    {

        //ListView1.FindControl("link_za_test").Visible = true;

        //HtmlControl htmlDivControl = (HtmlControl)Page.FindControl("link_za_tet");
        //htmlDivControl.Visible = true;
     }
}

Solution

  • The controls your are trying to access in code behind are inside an item template. For this reason, you cannot access them via intellisense because they are not actually on the form - they will be added when the parent control (ListView1) is bound.

    Subscribe the the item databound event of the listview control. In there, you can access the child controls for each item in the listview. Something like this would get you the control:

    e.Item.FindControl("TestLinkBox");