Last post May 21, 2020 06:19 AM by Yang Shen
May 20, 2020 11:29 PM|NJ2|LINK
The code below in asp.net c#, the function CheckUser never executes. What am I doing wrong?
protected void Page_Load(object sender, EventArgs e)
txtUserName.Attributes.Add("onblur", "return CheckUser(txtUserName.Text)");
private bool CheckUser(string sUser)
if (FindUser(sUser) == true)
May 21, 2020 01:26 AM|KathyW|LINK
that in a TextChanged event attached to the textbox.
May 21, 2020 04:30 AM|NJ2|LINK
Would you be able to provide me sample code?
May 21, 2020 06:19 AM|Yang Shen|LINK
First of all, onblur is a front end event, it will call the js method bound to it.
So you will need to define your CheckUser as a JS function rather than a C# one, otherwise it will have no effect.
And you put this event in the page_load, which means that the content of the text box will never change in the method.
And the OnTextChanged
could be a better choice in this scenario indeed, this event is used in the text box, you can refer to this example:
<form id="form1" runat="server">
<asp:TextBox ID="userNameTxt" runat="server"
<asp:Label ID="checkUserName" runat="server" />
private bool checkUser(String name)
if (name != null && !"".Equals(name) )
protected void userNameTxt_TextChanged(object sender, EventArgs e)
TextBox box = (TextBox)sender;
checkUserName.Text = "username is not empty";
checkUserName.Text = "username is empty";