writing a customControl

Discussion in 'ASP .Net' started by philipp, May 17, 2005.

  1. philipp

    philipp Guest

    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;

    }

    }
    philipp, May 17, 2005
    #1
    1. Advertising

  2. 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;
    >
    > }
    >
    > }
    >
    >
    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=, May 17, 2005
    #2
    1. Advertising

  3. philipp

    philipp Guest

    Thanks. It works.

    Philipp
    philipp, May 18, 2005
    #3
    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. Eric B
    Replies:
    1
    Views:
    1,861
    Eric B
    Sep 3, 2003
  2. KMILO
    Replies:
    0
    Views:
    361
    KMILO
    Apr 22, 2005
  3. =?Utf-8?B?U3RldmU=?=

    ImageButton Click Event in CustomControl

    =?Utf-8?B?U3RldmU=?=, Feb 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    910
    =?Utf-8?B?U3RldmU=?=
    Feb 20, 2006
  4. Ashish

    CustomControl Losing Events.

    Ashish, May 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Ashish
    May 22, 2006
  5. Angrez Singh
    Replies:
    0
    Views:
    327
    Angrez Singh
    Jun 30, 2006
Loading...

Share This Page