Can't access webpage's controls from the code-behind file after putting it in a <asp:LoginView/>

Discussion in 'ASP .Net Web Controls' started by Jack, Jul 3, 2006.

  1. Jack

    Jack Guest

    Hello,

    After I moved my Checkbox control 'inside' a <asp:LoginView />, my
    code-behind file isn't compiling saying that:

    "The name 'MyCheckbox' does not not exist in the current context."

    Basically, when the 'administrator', and only the administrator, logs into
    the site (from another page), I want him to check a checkbox on this page.
    It all worked fine with anonymous access before (without the username and
    roles set up). I'm not too sure why one shouldn't access the ASP controls
    from code.

    How can my code-behind file access ASP.NET 2.0 controls on my webpage when
    the controls are embedded inside a <asp:LoginView> ?

    It looks like this:

    <asp:LoginView ID="LoginView1" runat="server">
    <AnonymousTemplate>
    Anonymous template here...
    </AnonymousTemplate>

    <RoleGroups>
    <asp:RoleGroup Roles="Administrator">
    <ContentTemplate>

    <asp:LoginStatus ID="LoginStatus" runat="server"
    LogoutImageUrl="Graphics/logout.jpg" Visible="false" />

    <asp:Button ID="ButtonInsertNew" runat="server"
    Text="Insert New" OnClick="ButtonInsertNew_Click" />
    <asp:Label ID="LabelError" runat="server"
    ForeColor="Red" Width="625px"></asp:Label>

    <asp:panel ID="PanelFileUpload" runat="server" >
    <asp:CheckBox ID="CheckBoxPDF" runat="server"
    AutoPostBack="True"
    OnCheckedChanged="CheckBoxPDF_CheckedChanged"
    Text="PDF Filename" Width="125" />
    <asp:FileUpload ID="FileUploadPDF"
    runat="server" Enabled="False" />
    </asp:panel>

    </ContentTemplate>
    </asp:RoleGroup>

    </RoleGroups>
    </asp:LoginView>


    Thanks,
    Jack.
    Jack, Jul 3, 2006
    #1
    1. Advertising

  2. hi Jack, since your control is a child control of the loginview control, you
    cant access it directly. You need to use the findcontrol method on your
    loginview control and get a reference to the checkbox contained in the
    contenttemplate.
    for eg.
    c#
    CheckBox cb1 = (CheckBox)LoginView1.FindControl("MyCheckBox");
    if (cb1 != null)
    // do something with cb1

    vb.net
    Dim cb1 AS CheckBox = CType(LoginView1.FindControl("MyCheckBox"), CheckBox)
    if (cb1 IsNot Nothing) then
    'Lets do something
    end if

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net
    "Jack" <> ha scritto nel messaggio
    news:44a90959$...
    > Hello,
    >
    > After I moved my Checkbox control 'inside' a <asp:LoginView />, my
    > code-behind file isn't compiling saying that:
    >
    > "The name 'MyCheckbox' does not not exist in the current context."
    >
    > Basically, when the 'administrator', and only the administrator, logs into
    > the site (from another page), I want him to check a checkbox on this page.
    > It all worked fine with anonymous access before (without the username and
    > roles set up). I'm not too sure why one shouldn't access the ASP controls
    > from code.
    >
    > How can my code-behind file access ASP.NET 2.0 controls on my webpage when
    > the controls are embedded inside a <asp:LoginView> ?
    >
    > It looks like this:
    >
    > <asp:LoginView ID="LoginView1" runat="server">
    > <AnonymousTemplate>
    > Anonymous template here...
    > </AnonymousTemplate>
    >
    > <RoleGroups>
    > <asp:RoleGroup Roles="Administrator">
    > <ContentTemplate>
    >
    > <asp:LoginStatus ID="LoginStatus"
    > runat="server" LogoutImageUrl="Graphics/logout.jpg" Visible="false" />
    >
    > <asp:Button ID="ButtonInsertNew" runat="server"
    > Text="Insert New" OnClick="ButtonInsertNew_Click" />
    > <asp:Label ID="LabelError" runat="server"
    > ForeColor="Red" Width="625px"></asp:Label>
    >
    > <asp:panel ID="PanelFileUpload" runat="server" >
    > <asp:CheckBox ID="CheckBoxPDF" runat="server"
    > AutoPostBack="True"
    >
    > OnCheckedChanged="CheckBoxPDF_CheckedChanged"
    > Text="PDF Filename" Width="125" />
    > <asp:FileUpload ID="FileUploadPDF"
    > runat="server" Enabled="False" />
    > </asp:panel>
    >
    > </ContentTemplate>
    > </asp:RoleGroup>
    >
    > </RoleGroups>
    > </asp:LoginView>
    >
    >
    > Thanks,
    > Jack.
    >
    >
    Alessandro Zifiglio, Jul 3, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    570
    Alphonse Giambrone
    Jun 28, 2003
  2. Saurabh
    Replies:
    6
    Views:
    4,504
    Chris Smith
    May 30, 2004
  3. wikkiwikkiwaa
    Replies:
    2
    Views:
    12,556
    wikkiwikkiwaa
    Aug 9, 2006
  4. David A. Osborn

    Can't access controls in code behind

    David A. Osborn, Sep 30, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    902
    Laurent Bugnion
    Oct 2, 2006
  5. LLars
    Replies:
    2
    Views:
    272
    LLars
    Feb 14, 2006
Loading...

Share This Page