By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

can't get control to hold state

P: n/a
I'm trying to create a login box control to use that simply exposes the
username, password and remember me properties when the login button is
clicked. I have the button click wired up and working and at one time,
the username was holding state. I don't know what I changed, but now
nothing holds state. I've read a number of messages about making sure
that the controls are added to the control collection during
CreateChildControls, which I do. So, I can't figure out what else to
do to make it hold state. I did notice though that onpostback, none of
the set methods of a property were being called. I haven't done much
web control development, so I'm still learning a lot of this. But, I
don't want the end user to have to worry about setting viewstate values
or anything. I want to be able to just drop this on a page, post it
and get back the username, password and remember me values. Can anyone
see what's wrong with my code? Thanks. I left out a few of the
properties where you set up CSS and such for brevity.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SDSLogin.Web.Controls
{
[DefaultProperty("Username")]
[ToolboxData("<{0}:LoginBox runat=server></{0}:LoginBox>")]
public class LoginBox : WebControl
{
public event EventHandler Click;

/// <summary>
/// Pass the click event back to the containing page to be
handled.
/// </summary>
/// <param name="sender">The sender of the event</param>
/// <param name="e">An EventArgs object.</param>
protected virtual void OnBtnLink_Click(object sender, EventArgs
e)
{
if (Click != null)
{
CreateChildControls();
Click(this, e);
}
}

private string _pwd = string.Empty;
private string _username = string.Empty;
private string _usernamelabel = "Username";
private string _pwdlabel = "Password";
private string _btnText = "Login";
private string _txtUserTBCss = string.Empty;
private string _txtPwdTBCss = string.Empty;
private string _chkCss = string.Empty;
private string _lblUserCss = string.Empty;
private string _lblPwdCss = string.Empty;

#region controls
private TextBox tUser;
private TextBox tPwd;
private Label lUser;
private Label lPwd;
private Literal spacer;
private Button bSubmit;
private Table t;
private TableRow trUser;
private TableCell tcUser;
private TableRow trPwd;
private TableCell tcPwd;
private TableRow trRemember;
private TableCell tcRemember;
private CheckBox cRemember;
#endregion

private bool _chk = false;
private bool _remember = false;

private System.Web.UI.WebControls.Unit _tablewidth = 300;

private int _cellpadding = 3;
private int _cellspacing = 3;
private int _tableborder = 0;

#region properties
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public bool CheckRemember
{
get {
_chk = (bool)ViewState["RememberChecked"];
this.EnsureChildControls();
return _chk;
}
set {
ViewState["RememberChecked"] = _chk;
this.EnsureChildControls();
cRemember.Checked = _chk;
_chk = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Username
{
get
{
_username = (String)ViewState["Username"];
if (_username == null)
_username = string.Empty;
EnsureChildControls();
return _username;
}

set
{
ViewState["Username"] = value;
this.EnsureChildControls();
tUser.Text = value;
_username = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Password
{
get
{
_pwd = (string)ViewState["Password"];
if (_pwd == null)
_pwd = string.Empty;
this.EnsureChildControls();
return _pwd;
}

set
{
ViewState["Password"] = value;
this.EnsureChildControls();
tPwd.Text = value;
_pwd = value;
}
}

#endregion

protected override void CreateChildControls()
{
tUser = new TextBox();
tPwd = new TextBox();
lUser = new Label();
lPwd = new Label();
spacer = new Literal();
bSubmit = new Button();
t = new Table();
trUser = new TableRow();
tcUser = new TableCell();
trPwd = new TableRow();
tcPwd = new TableCell();
trRemember = new TableRow();
tcRemember = new TableCell();
cRemember = new CheckBox();
spacer.Text = "&nbsp;&nbsp;";

bSubmit.Click += new EventHandler(this.OnBtnLink_Click);

//add controls
tcUser.Controls.Add(lUser);
tcUser.Controls.Add(tUser);
trUser.Cells.Add(tcUser);
t.Rows.Add(trUser);

tcPwd.Controls.Add(lPwd);
tcPwd.Controls.Add(tPwd);
tcPwd.Controls.Add(spacer);

tcPwd.Controls.Add(bSubmit);
trPwd.Cells.Add(tcPwd);
t.Rows.Add(trPwd);

tcRemember.Controls.Add(cRemember);
trRemember.Cells.Add(tcRemember);
t.Rows.Add(trRemember);

Controls.Add(t);
}

#region RenderContents
protected override void RenderContents(HtmlTextWriter output)
{
t.CellPadding = _cellpadding;
t.CellSpacing = _cellspacing;
t.BorderWidth = _tableborder;
t.Width = _tablewidth;

//create username row
tUser.CssClass = _txtUserTBCss;
tUser.Style.Add("width", "99%");

lUser.Text = _usernamelabel + "<br />";
lUser.CssClass = _lblUserCss;
if (_username.Length > 0)
tUser.Text = _username;

//create password row
tPwd.CssClass = _txtPwdTBCss;
tPwd.TextMode = TextBoxMode.Password;
if (_password.Length > 0)
tPwd.Text = this._password;
tPwd.Style.Add("width", "65%");
lPwd.Text = _pwdlabel + "<br />";
lPwd.CssClass = _lblPwdCss;

bSubmit.Text = _btnText;

//create remember row
if (EnableRemember)
{
cRemember.Text = "Remember Me";
cRemember.CssClass = _chkCss;
cRemember.Checked = _chk;
}
else
{
trRemember.Visible = false;
}
t.RenderControl(output);
//output.Write(Text);
}
#endregion


}
}

May 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I figured out that the section that says

{
if (Click != null)
{
CreateChildControls();
Click(this, e);
}
}

was causing a problem. I removed the CreateChildControls and now my
username field holds it's state. However, I still can't get the
checkbox to hold it's state. If I change the password field from
password mode to normal text mode, it does hold it's state. Is there a
trick to getting a checkbox to hold it's state? Thanks.

May 3 '06 #2

P: n/a
I found another problem. It seems that when I try to access the values
in my click event, they are empty and when the page renders, they don't
appear in the textboxes. But, if I don't try to access the values in
the click event, then the textboxes hold state. So, the following code
doesn't hold state...

protected void lb1_Click(object sender, EventArgs e)
{
Response.Write(lb.Username + " " + lb.Password + " " +
lb.RememberMe);
Response.Write("yo");
}

....and this does...

protected void lb1_Click(object sender, EventArgs e)
{
//Response.Write(lb.Username + " " + lb.Password + " " +
lb.RememberMe);
Response.Write("yo");
}

Why? BTW, if you're following the code, I changed the CheckRemember
property to RememberMe so it was a little clearer for the end user.

May 3 '06 #3

P: n/a
anyone? please?

May 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.