Need Javascript - if user clicks in textbox, label not visible

G

Guest

Hello -

I have a form that when submitted checks the database and if the username is
already taken, a label shows indicating same.

I need to make that label NOT visible after the user clicks in the username
textbox to change the name.

Any help will be greatly appreciated!
 
G

Guest

John -

Thanks for your response.

Sorry for being dense, but I know absolutely nothing about javascript - it
is the next thing on my agenda to learn.

Can you tell me where and how I would put this in my code?
 
D

Dave Fancher

I prefer the option of just hiding it.

Assuming that your TextBox is named userName and your Label is named
errorLabel you could use the following code to add the client side event
handler to the TextBox. I would recommend placing this code somewhere where
it will only be executed when the label is going to be marked as visible.

[C#]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = \"none\";");

[VB - sorry if my syntax is slightly off]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = ""none"";");

HTH
 
D

Dave Fancher

Oops! Take the last 's' off of 'styles' in the examples. I also realized
after my post that I added a line-end semi-colon in the VB example.

[C#]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').style.display = \"none\";");

[VB - sorry if my syntax is slightly off]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').style.display = ""none"";")

Sorry for the mix-up!

HTH
----------------
Dave Fancher
http://www.davefancher.com

Dave Fancher said:
I prefer the option of just hiding it.

Assuming that your TextBox is named userName and your Label is named
errorLabel you could use the following code to add the client side event
handler to the TextBox. I would recommend placing this code somewhere
where it will only be executed when the label is going to be marked as
visible.

[C#]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = \"none\";");

[VB - sorry if my syntax is slightly off]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = ""none"";");

HTH
 
G

Guest

Dave -

Thanks so much for your response! That sure looks like it should work, but
it didn't. The lblMessage (which indicates the name is taken) did not
disappear when I put the cursor in the textbox, or even after I typed in the
textbox and tabbed to the next textbox.

Could I trouble you to take a look at the code? I put your solution at the
end of the sub.

Private Sub btnSubmitNewMember_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSubmitNewMember.Click

'Check if User Name taken
Dim daTest as SqlDataAdapter
Dim dsLogin as New DataSet
daTest = New SqlDataAdapter("Select Count(UserID) as TheCount " _
& "from tblLogin Where " _
& "UserName = '" & txtNewUserName.Text _
& "'", cnn)
daTest.Fill(dsLogin, "TheCount")

Dim NewValue as Integer
NewValue = dsLogin.Tables("TheCount").Rows(0).Item("TheCount")
'If User Name is taken, it will return a 1; otherwise if not taken, will
return 0 and User Name is okay
If dsLogin.Tables("TheCount").Rows(0).Item("TheCount") = 0 Then

Dim cmdLogin as SqlCommand = cnn.CreateCommand
cmdLogin.CommandType = CommandType.StoredProcedure
cmdLogin.CommandText = "spTblLoginOnly"

cmdLogin.Parameters.Add(New SqlParameter("@UserName", SqlDbType.Char, 50))
cmdLogin.Parameters("@UserName").Value = txtNewUserName.Text
cmdLogin.Parameters.Add(New SqlParameter("@Password", SqlDbType.Char, 15))
cmdLogin.Parameters("@Password").Value = txtNewPassword.Text
cmdLogin.Parameters.Add(New SqlParameter("@EmailAddress",
SqlDbType.Char, 60))
cmdLogin.Parameters("@EmailAddress").Value = txtEmail.Text
cmdLogin.Parameters.Add(New SqlParameter("@ScreenName", SqlDbType.Char,
15))
cmdLogin.Parameters("@ScreenName").Value = txtScreenName.Text

cmdLogin.Parameters.Add(New SqlParameter("@UserID", SqlDbType.Int))
cmdLogin.Parameters("@UserID").Direction = ParameterDirection.Output
cnn.Open
cmdLogin.ExecuteNonQuery()
'Get the UserName from tblLogin and put it into Session
daTest = New SqlDataAdapter("Select UserID from tblLogin Where " _
& "UserName = '" & txtNewUserName.Text _
& "' and Password = '" & txtNewPassword.Text _
& "'", cnn)
daTest.Fill(dsLogin, "tblLogin")
Session("UserID") = dsLogin.Tables("tblLogin").Rows(0).Item("UserID")

cnn.Close

Response.Redirect(HttpUtility.UrlDecode(Request.QueryString("currpage")))

Else
pnlNewMember.Visible = True

lblMessage.Text = "The User Name you entered is already in use. Please
choose a different User Name."
txtNewUserName.Attributes.Add("onfocus", "document.getElementById('" +
lblMessage.UniqueID + "').styles.display =""none"";")

End If

End Sub

What am I doing wrong?


--
Sandy


Dave Fancher said:
I prefer the option of just hiding it.

Assuming that your TextBox is named userName and your Label is named
errorLabel you could use the following code to add the client side event
handler to the TextBox. I would recommend placing this code somewhere where
it will only be executed when the label is going to be marked as visible.

[C#]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = \"none\";");

[VB - sorry if my syntax is slightly off]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = ""none"";");

HTH
 
D

Dave Fancher

You probably didn't see my reply to myself. I accidentally put an 's' on
the end of 'style' so it wouldn't work. Using this line instead should
clear up the problem for you. Sorry for the typo!

txtNewUserName.Attributes.Add("onfocus", "document.getElementById('" +
lblMessage.UniqueID + "').style.display = ""none"";")

I also edited the line in your code below.

HTH
----------------
Dave Fancher
http://www.davefancher.com

Sandy said:
Dave -

Thanks so much for your response! That sure looks like it should work,
but
it didn't. The lblMessage (which indicates the name is taken) did not
disappear when I put the cursor in the textbox, or even after I typed in
the
textbox and tabbed to the next textbox.

Could I trouble you to take a look at the code? I put your solution at
the
end of the sub.

Private Sub btnSubmitNewMember_Click(ByVal sender As System.Object, ByVal
e
As System.EventArgs) Handles btnSubmitNewMember.Click

'Check if User Name taken
Dim daTest as SqlDataAdapter
Dim dsLogin as New DataSet
daTest = New SqlDataAdapter("Select Count(UserID) as TheCount " _
& "from tblLogin Where " _
& "UserName = '" & txtNewUserName.Text _
& "'", cnn)
daTest.Fill(dsLogin, "TheCount")

Dim NewValue as Integer
NewValue = dsLogin.Tables("TheCount").Rows(0).Item("TheCount")
'If User Name is taken, it will return a 1; otherwise if not taken,
will
return 0 and User Name is okay
If dsLogin.Tables("TheCount").Rows(0).Item("TheCount") = 0 Then

Dim cmdLogin as SqlCommand = cnn.CreateCommand
cmdLogin.CommandType = CommandType.StoredProcedure
cmdLogin.CommandText = "spTblLoginOnly"

cmdLogin.Parameters.Add(New SqlParameter("@UserName", SqlDbType.Char,
50))
cmdLogin.Parameters("@UserName").Value = txtNewUserName.Text
cmdLogin.Parameters.Add(New SqlParameter("@Password", SqlDbType.Char,
15))
cmdLogin.Parameters("@Password").Value = txtNewPassword.Text
cmdLogin.Parameters.Add(New SqlParameter("@EmailAddress",
SqlDbType.Char, 60))
cmdLogin.Parameters("@EmailAddress").Value = txtEmail.Text
cmdLogin.Parameters.Add(New SqlParameter("@ScreenName", SqlDbType.Char,
15))
cmdLogin.Parameters("@ScreenName").Value = txtScreenName.Text

cmdLogin.Parameters.Add(New SqlParameter("@UserID", SqlDbType.Int))
cmdLogin.Parameters("@UserID").Direction = ParameterDirection.Output
cnn.Open
cmdLogin.ExecuteNonQuery()
'Get the UserName from tblLogin and put it into Session
daTest = New SqlDataAdapter("Select UserID from tblLogin Where " _
& "UserName = '" & txtNewUserName.Text _
& "' and Password = '" & txtNewPassword.Text _
& "'", cnn)
daTest.Fill(dsLogin, "tblLogin")
Session("UserID") = dsLogin.Tables("tblLogin").Rows(0).Item("UserID")

cnn.Close


Response.Redirect(HttpUtility.UrlDecode(Request.QueryString("currpage")))

Else
pnlNewMember.Visible = True

lblMessage.Text = "The User Name you entered is already in use. Please
choose a different User Name."
txtNewUserName.Attributes.Add("onfocus", "document.getElementById('" +
lblMessage.UniqueID + "').style.display =""none"";")

End If

End Sub

What am I doing wrong?


--
Sandy


Dave Fancher said:
I prefer the option of just hiding it.

Assuming that your TextBox is named userName and your Label is named
errorLabel you could use the following code to add the client side event
handler to the TextBox. I would recommend placing this code somewhere
where
it will only be executed when the label is going to be marked as visible.

[C#]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = \"none\";");

[VB - sorry if my syntax is slightly off]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = ""none"";");

HTH
----------------
Dave Fancher
http://www.davefancher.com

Sandy said:
John -

Thanks for your response.

Sorry for being dense, but I know absolutely nothing about javascript -
it
is the next thing on my agenda to learn.

Can you tell me where and how I would put this in my code?


--
Sandy


:

Use CSS in a javascript onfocus event

http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onfocus.asp

labelID.style.visibility = ' hidden ';

or move the label off screen with javascript

function moveTo(x, y) {
this.element.left = x;
this.element.top = y;
}

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Hello -

I have a form that when submitted checks the database and if the
username
is
already taken, a label shows indicating same.

I need to make that label NOT visible after the user clicks in the
username
textbox to change the name.

Any help will be greatly appreciated!
 
G

Guest

Thank you so much, Dave.

It works beautifully!!!

I really appreciate your time and patience!

--
Sandy


Dave Fancher said:
You probably didn't see my reply to myself. I accidentally put an 's' on
the end of 'style' so it wouldn't work. Using this line instead should
clear up the problem for you. Sorry for the typo!

txtNewUserName.Attributes.Add("onfocus", "document.getElementById('" +
lblMessage.UniqueID + "').style.display = ""none"";")

I also edited the line in your code below.

HTH
----------------
Dave Fancher
http://www.davefancher.com

Sandy said:
Dave -

Thanks so much for your response! That sure looks like it should work,
but
it didn't. The lblMessage (which indicates the name is taken) did not
disappear when I put the cursor in the textbox, or even after I typed in
the
textbox and tabbed to the next textbox.

Could I trouble you to take a look at the code? I put your solution at
the
end of the sub.

Private Sub btnSubmitNewMember_Click(ByVal sender As System.Object, ByVal
e
As System.EventArgs) Handles btnSubmitNewMember.Click

'Check if User Name taken
Dim daTest as SqlDataAdapter
Dim dsLogin as New DataSet
daTest = New SqlDataAdapter("Select Count(UserID) as TheCount " _
& "from tblLogin Where " _
& "UserName = '" & txtNewUserName.Text _
& "'", cnn)
daTest.Fill(dsLogin, "TheCount")

Dim NewValue as Integer
NewValue = dsLogin.Tables("TheCount").Rows(0).Item("TheCount")
'If User Name is taken, it will return a 1; otherwise if not taken,
will
return 0 and User Name is okay
If dsLogin.Tables("TheCount").Rows(0).Item("TheCount") = 0 Then

Dim cmdLogin as SqlCommand = cnn.CreateCommand
cmdLogin.CommandType = CommandType.StoredProcedure
cmdLogin.CommandText = "spTblLoginOnly"

cmdLogin.Parameters.Add(New SqlParameter("@UserName", SqlDbType.Char,
50))
cmdLogin.Parameters("@UserName").Value = txtNewUserName.Text
cmdLogin.Parameters.Add(New SqlParameter("@Password", SqlDbType.Char,
15))
cmdLogin.Parameters("@Password").Value = txtNewPassword.Text
cmdLogin.Parameters.Add(New SqlParameter("@EmailAddress",
SqlDbType.Char, 60))
cmdLogin.Parameters("@EmailAddress").Value = txtEmail.Text
cmdLogin.Parameters.Add(New SqlParameter("@ScreenName", SqlDbType.Char,
15))
cmdLogin.Parameters("@ScreenName").Value = txtScreenName.Text

cmdLogin.Parameters.Add(New SqlParameter("@UserID", SqlDbType.Int))
cmdLogin.Parameters("@UserID").Direction = ParameterDirection.Output
cnn.Open
cmdLogin.ExecuteNonQuery()
'Get the UserName from tblLogin and put it into Session
daTest = New SqlDataAdapter("Select UserID from tblLogin Where " _
& "UserName = '" & txtNewUserName.Text _
& "' and Password = '" & txtNewPassword.Text _
& "'", cnn)
daTest.Fill(dsLogin, "tblLogin")
Session("UserID") = dsLogin.Tables("tblLogin").Rows(0).Item("UserID")

cnn.Close


Response.Redirect(HttpUtility.UrlDecode(Request.QueryString("currpage")))

Else
pnlNewMember.Visible = True

lblMessage.Text = "The User Name you entered is already in use. Please
choose a different User Name."
txtNewUserName.Attributes.Add("onfocus", "document.getElementById('" +
lblMessage.UniqueID + "').style.display =""none"";")

End If

End Sub

What am I doing wrong?


--
Sandy


Dave Fancher said:
I prefer the option of just hiding it.

Assuming that your TextBox is named userName and your Label is named
errorLabel you could use the following code to add the client side event
handler to the TextBox. I would recommend placing this code somewhere
where
it will only be executed when the label is going to be marked as visible.

[C#]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = \"none\";");

[VB - sorry if my syntax is slightly off]
userName.Attributes.Add("onfocus", "document.getElementById('" +
errorLabel.UniqueID + "').styles.display = ""none"";");

HTH
----------------
Dave Fancher
http://www.davefancher.com

John -

Thanks for your response.

Sorry for being dense, but I know absolutely nothing about javascript -
it
is the next thing on my agenda to learn.

Can you tell me where and how I would put this in my code?


--
Sandy


:

Use CSS in a javascript onfocus event

http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onfocus.asp

labelID.style.visibility = ' hidden ';

or move the label off screen with javascript

function moveTo(x, y) {
this.element.left = x;
this.element.top = y;
}

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Hello -

I have a form that when submitted checks the database and if the
username
is
already taken, a label shows indicating same.

I need to make that label NOT visible after the user clicks in the
username
textbox to change the name.

Any help will be greatly appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top