UserControl, use it in .cs file

C

Christian Ista

Hello,

In an aspx page (default.aspx in the HTML), I use an uercontrol

<%@ Register TagPrefix="CTest" TagName="Test" Src="~\controls\Login.ascx" %>
......
<CTest:Testid="MyTest" runat="server"></CTest:Test>
......

no problem when I start this application, I see it.

Now I'd like hide this control when I click on a button, how can I have
access, in the code-behind, to hide this UserControl ?

That's not work when I do this :

private void Button1_Click(object sender, System.EventArgs e)
{
MyTest.Visible = false;
}

Thanks,

Christian,
 
B

Bryce Budd

It will work as expected if you put declare the usercontrol in your
code-behind. Similar to what happens if you drag a server control onto a
webform.

HTH
Bryce
 
C

Christian Ista

It will work as expected if you put declare the usercontrol in your
code-behind. Similar to what happens if you drag a server control onto a
webform.

Ok but I don't know the code to use.

For a button, I have this :
protected System.Web.UI.WebControls.Button Button1;

But I don't see the code to use. The usercontrols is in ~/controls/Test.ascx

Thanks,


Christian,
 
K

Kumar Reddi

You have to add the code for it in the code behind

protected Login MyTest; //assuming your user control's class name is Login

you are good to go
 
C

Christian Ista

protected Login MyTest; //assuming your user control's class name is Login

I tried of course with this code :
protected Login MyLogin;

but I have this erreur:
The type or namespace name 'Login' could not be found (are you missing a
using directive or an assembly reference?)

Is it because the control is not in the main directory but in the dir
control ?


Thanks,


Christian,
 
K

Kumar Reddi

Yeah, its because, your Login control is in another folder. You should
provide the using statement, for your code behind to access that. In your
user controls code behind, the namespace you create should be
yourProjectNamespace.Controls

Then in your aspx code behind you should specify a using statement as
follows

using yourProjectNamespace.Controls;

Then you should be able to access classes in the Controls folder

Good Luck
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top