473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

can't get control to hold state

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
3 1946
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
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
anyone? please?

May 4 '06 #4

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

Similar topics

4
by: Daylor | last post by:
in win32 process , when u create new process,u have new main thread. i know,appDomain r logical procces,that exists in 1 win32 process. the q: is there way to create second appDomain (the...
1
by: ca___t | last post by:
Hi there: I have a web image control and a web button in my aspx page. Then I change its src property use by javascript at client side. But I cann't get change value of image control when I...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
0
by: Steven | last post by:
When I set my ASP.NET project IIS directory security with: Anonymous Access checked and a valid domain account is supplied; and Integrated Security is unchecked - none of the state changes in my...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.