writing a customControl

P

philipp

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;

}

}
 
G

Guest

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 said:
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;

}

}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top