I've got a custom control as listed below (in C#)
When i put it on my page i can change the username and password properties at design time.Code:public class LoginControl : System.Web.UI.WebControls.WebControl { private System.Web.UI.WebControls.TextBox txtusername = new TextBox(); private System.Web.UI.WebControls.TextBox txtpassword = new TextBox(); private System.Web.UI.WebControls.Button cmdLogon = new Button(); private System.Web.UI.WebControls.RequiredFieldValidator reqUsername = new RequiredFieldValidator(); private System.Web.UI.WebControls.RegularExpressionValidator revPassword = new RegularExpressionValidator(); [Bindable(true),Category("TextBoxContents")] public string Username { get { return txtusername.Text; } set { txtusername.Text = value; } } public string Password{ get { return txtpassword.Text; } set { txtpassword.Text = value; } } public LoginControl() { txtusername.Width = 140; txtpassword.Width = 140; txtusername.MaxLength = 15; txtpassword.MaxLength = 15; txtusername.TextMode = TextBoxMode.SingleLine; txtpassword.TextMode = TextBoxMode.Password; cmdLogon.Text = "Logon"; cmdLogon.Attributes.Add("OnServerClick","Submit_Click"); //reqUsername.Text = "Username must not be blank"; reqUsername.Display = ValidatorDisplay.Dynamic; reqUsername.EnableClientScript = true; reqUsername.ErrorMessage = "Username must not be blank"; reqUsername.ControlToValidate = "txtusername"; reqUsername.Visible = false; revPassword.Display = ValidatorDisplay.Dynamic; revPassword.EnableClientScript = true; revPassword.ErrorMessage = "Password must be at least 6 alpha-numeric charactors"; revPassword.ControlToValidate = "txtpassword"; revPassword.Visible = false; } /// <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>"); } }
Page code is below:
If i change them at run time it doesn't work. It's as if it's still retrieving the old value (blank) when i do ctrlLogin.UsernameCode:<%@ Page Language="C#" %> <%@ Register TagPrefix="n0" Namespace="George" Assembly="LoginControl" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <%@ import Namespace="System.Web.Security" %> <script runat="server"> private void Page_Load(Object sender, EventArgs E) { Page.Validate(); if ((Page.IsPostBack) && (Page.IsValid)) { SqlConnection myConnection = new SqlConnection("server=GEORGE;database=MyDB;Trusted_Connection=yes"); SqlCommand myCommand = new SqlCommand("SELECT People.Nickname, People.Password FROM People WHERE People.Nickname='" + ctrlLogin.Username + "'", myConnection); try { myConnection.Open(); SqlDataReader dr = myCommand.ExecuteReader(); if(dr.Read()) { if(dr.GetString(1).Trim() == ctrlLogin.Password.Trim()) FormsAuthentication.RedirectFromLoginPage(ctrlLogin.Username, false); else Message.Text = "Sorry! Your password is incorrect.</br>Please log in again."; } else Message.Text = "Your username is wrong!</br>Please log in again."; } catch(Exception myException) { Response.Write("Oops! The error: " + myException.Message + " occured."); } finally { myConnection.Close(); } Message.Text = "SELECT People.Nickname, People.Password FROM People WHERE People.Nickname='" + ctrlLogin.Username + "'"; } } </script> <html> <head> </head> <body style="FONT-FAMILY: arial"> <form runat="server"> <p> <n0:LoginControl id="ctrlLogin" runat="server"></n0:LoginControl> </p> <p> <asp:Label id="Message" runat="server">Label</asp:Label> </p> </form> </body> </html>
If i fill in the username and password at design time it works ok, however if i leave them blank, and type them in at runtime it doesn't work.
Any ideas why this is happening?





Reply With Quote