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

J

Jack

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.
 
A

Alessandro Zifiglio

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top