|
-
Apr 23rd, 2005, 10:13 AM
#3
Thread Starter
Not NoteMe
Re: Custom control Properties
Thanks for your reply.
I've changed the code as per your recommendations, listed below:
Code:
public class LoginControl : System.Web.UI.WebControls.WebControl
{
private const string _usernameStateKey = "usrnm";
private const string _passwordStateKey = "pswrd";
private string _username;
private string _password;
private System.Web.UI.WebControls.TextBox txtusername = null;
private System.Web.UI.WebControls.TextBox txtpassword = null;
private System.Web.UI.WebControls.Button cmdLogon = null;
private System.Web.UI.WebControls.RequiredFieldValidator reqUsername = null;
private System.Web.UI.WebControls.RegularExpressionValidator revPassword = null;
[Bindable(true),Category("TextBoxContents")]
public string Username
{
get
{
if (ViewState[_usernameStateKey] != null)
return (string)ViewState[_usernameStateKey];
else
return _username;
}
set
{
_username = value;
ViewState.Add(_usernameStateKey,value);
}
}
public string Password
{
get
{
if (ViewState[_passwordStateKey] != null)
return (string)ViewState[_passwordStateKey];
else
return _password;
}
set
{
_password = value;
ViewState.Add(_passwordStateKey, value);
}
}
public new void PreRender()
{
EnsureChildControls();
txtusername.Text = ViewState[_usernameStateKey] == null ? _username : (string) ViewState[_usernameStateKey];
txtpassword.Text = ViewState[_passwordStateKey] == null ? _password : (string) ViewState[_passwordStateKey];
}
protected override void CreateChildControls()
{
txtusername = new TextBox();
txtpassword = new TextBox();
cmdLogon = new Button();
reqUsername = new RequiredFieldValidator();
revPassword = new RegularExpressionValidator();
txtusername.ID = "txtusername";
txtpassword.TextMode = TextBoxMode.Password;
cmdLogon.Text = "Logon";
txtusername.Width = 140;
txtpassword.Width = 140;
txtusername.MaxLength = 15;
txtpassword.MaxLength = 15;
txtpassword.TextMode = TextBoxMode.Password;
txtpassword.ID = "txtpassword";
cmdLogon.Text = "Logon";
cmdLogon.Attributes.Add("OnServerClick","Submit_Click");
reqUsername.Display = ValidatorDisplay.Dynamic;
reqUsername.EnableClientScript = true;
reqUsername.ErrorMessage = "Username must not be blank";
reqUsername.ControlToValidate = "txtusername";
revPassword.Display = ValidatorDisplay.Dynamic;
revPassword.EnableClientScript = true;
revPassword.ErrorMessage = "Password must be at least 6 alpha-numeric charactors";
revPassword.ControlToValidate = "txtpassword";
this.Controls.Add(txtusername);
this.Controls.Add(txtpassword);
this.Controls.Add(reqUsername);
this.Controls.Add(revPassword);
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
output.Write("<table border=\"1\" width=\"300\" id=\"table1\">");
output.Write("<tr border=\"0\">");
output.Write("<td border=\"0\">Username:</td>");
output.Write("<td border=\"0\">");
txtusername.RenderControl(output);
output.Write("</td>");
output.Write("</tr>");
output.Write("<tr border=\"0\">");
output.Write("<td border=\"0\">Password:</td>");
output.Write("<td border=\"0\">");
txtpassword.RenderControl(output);
output.Write("</td>");
output.Write("</tr>");
output.Write("<tr border=\"0\"><td colspan=\"2\"><div align=\"centre\">");
cmdLogon.RenderControl(output);
output.Write("<br>");
reqUsername.RenderControl(output);
output.Write("<br>");
revPassword.RenderControl(output);
output.Write("</div></td></tr>");
output.Write("</table>");
}
}
However, i still get the same problem, except that now if i change the property at design time, when running it i get an object reference not set error.
Any help would be greatly appreciated.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|