473,473 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

writing a customControl

Hi

I want to write a customControl, which shows a login-component
containing textboxes for the username and password and a login-button.
As soon as a user logs in, the textboxes and the button disappear and a
new button for a logout appears. But it didn't work. I have to press
the button twice to change it from login to logout and vice verca.

Can anybody help me? Thanks in advance.

Philipp

Here is the code of my CustomControl

public class loginControl : Control, INamingContainer
{
private string text;
private TextBox username;
private TextBox password;
private TextBox loggedIn;
private Button b, b2;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public bool Login
{
get
{
this.EnsureChildControls();
if(loggedIn.Text=="")
{
return false;
}
return Convert.ToBoolean(loggedIn.Text);
}

set
{
this.EnsureChildControls();
loggedIn.Text=value.ToString();
//login = value;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls ();

username=new TextBox();
password=new TextBox();
loggedIn=new TextBox();
loggedIn.Visible=false;
this.Controls.Add(loggedIn);
b=new Button();
b.Click += new EventHandler(this.loginBtn_Click);
b.Text="login";

b2=new Button();
b2.Click += new EventHandler(this.logoutBtn_Click);
b2.Text="logout";

if(!Login)
{
b2.Visible=false;
b.Visible=true;
}
else
{
b.Visible=false;
b2.Visible=true;
}

this.Controls.Add(username);
this.Controls.Add(password);

this.Controls.Add(b);
this.Controls.Add(b2);

}

private void loginBtn_Click(Object sender, EventArgs e)
{
this.Login=true;

}
private void logoutBtn_Click(Object sender, EventArgs e)
{
this.Login=false;

}

}

Nov 19 '05 #1
2 1368
hi philipp,

Basically you should write all the logic to manipulate/change a custom
controls appearance like hiding/showing and similar functions by overriding
the Render method.
CreateChildControls method is called everytime you page is loaded/reloaded,
so your control is created as a new one everytime so it will display the
default layout, the render method is responsiblr for rendering the layout of
the control, so if u write the logic here, it sould work.
If you want more info on this, check out the web control life cycle stages
to get a clear understanding of these concepts.

in your example override the Render method and remove the hiding/showing
logic from create child controls and add it as shown below in the Render
method.
Make sure u add the last line base.Render(output);, else you will not see
the control on the page.

protected override void Render(HtmlTextWriter output)
{
if(!Login)
{
b2.Visible=false;
b.Visible=true;
}
else
{
b.Visible=false;
b2.Visible=true;
}
base.Render(output);
}

Hope this helped you.
Regds
Kannan.V

"philipp" wrote:
Hi

I want to write a customControl, which shows a login-component
containing textboxes for the username and password and a login-button.
As soon as a user logs in, the textboxes and the button disappear and a
new button for a logout appears. But it didn't work. I have to press
the button twice to change it from login to logout and vice verca.

Can anybody help me? Thanks in advance.

Philipp

Here is the code of my CustomControl

public class loginControl : Control, INamingContainer
{
private string text;
private TextBox username;
private TextBox password;
private TextBox loggedIn;
private Button b, b2;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public bool Login
{
get
{
this.EnsureChildControls();
if(loggedIn.Text=="")
{
return false;
}
return Convert.ToBoolean(loggedIn.Text);
}

set
{
this.EnsureChildControls();
loggedIn.Text=value.ToString();
//login = value;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls ();

username=new TextBox();
password=new TextBox();
loggedIn=new TextBox();
loggedIn.Visible=false;
this.Controls.Add(loggedIn);
b=new Button();
b.Click += new EventHandler(this.loginBtn_Click);
b.Text="login";

b2=new Button();
b2.Click += new EventHandler(this.logoutBtn_Click);
b2.Text="logout";

if(!Login)
{
b2.Visible=false;
b.Visible=true;
}
else
{
b.Visible=false;
b2.Visible=true;
}

this.Controls.Add(username);
this.Controls.Add(password);

this.Controls.Add(b);
this.Controls.Add(b2);

}

private void loginBtn_Click(Object sender, EventArgs e)
{
this.Login=true;

}
private void logoutBtn_Click(Object sender, EventArgs e)
{
this.Login=false;

}

}

Nov 19 '05 #2
Thanks. It works.

Philipp

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
5
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long...
1
by: Eric B | last post by:
Hi, I've made a serverside component that automatically adds a validator. Currently i have both a REQUIRED validator and a CUSTOM validator in it. The customValidator has a javascript function...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
1
by: Sagaert Johan | last post by:
Hi How can i add an icon to my customcontrol so it appears in the toolbox ? How is this done in VS2005 ?
0
by: Angrez Singh | last post by:
Hi, I am trying to use a "customcontrol" inside a "webusercontrol" but facing problem with the viewstate of the "customcontrol". When I use the "customcontrol" on a page either adding it...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
89
by: Skybuck Flying | last post by:
Hello, This morning I had an idea how to write Scalable Software in general. Unfortunately with Delphi 2007 it can't be done because it does not support operating overloading for classes, or...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.