Friday, March 16, 2012

On Focus...

I have a Login teextbox and I want it to be populated with "Please enter username" or such upon the page loading, no issues there. But when the user tabs or clicks into the textbox I want it to clear the text so its ready for entry. I didnt find anything on a quick search so is there some event that I am missing that I can use for this?

The textbox control does not have a GotFocus event os I am unsure on this.

ThanksThat is a client side event, You will have to use Javascript to do that.
Not GotFocus. It's onFocus.

In the onFocus event, call a client side javascript method that first compares the text in that textbox with 'Please enter username'. If they match, then set the textbox's value to an empty string. Else, let it be.
I suppose I should create an external js file for all the js code that will be coming in the days ahead.

Do you have an outline of the js event? I hate js. Should I pass the control name and its value to the procedure that is going to be called? or can it be done some other way?
And In case you just too lazy to write the function here is an example <asp:TextBox id="TextBox1" runat="server" value = "Please Enter Username" onFocus = "changeVal()"></asp:TextBox>
<script type="text/javascript" language="JavaScript">
function changeVal() {
if (document.Form1.TextBox1.value == "Please Enter Username")
document.Form1.TextBox1.value = "";
}
</script>
JS is quite loose. Shuja's shown you how to do this. In the case of this particular function, you wouldn't pass the control's name because it's just a matter of ONE control being processed. If it were a whole bunch of controls, you would pass 'this' as an argument.

Adding it to a .JS file is your discretion. You may want to start by having it on the same page itself at first and then move it all out later.

Look out for case sensitivity. It can invoke hours of debugging time.
I suppose I should create an external js file for all the js code that will be coming in the days ahead. You can put all Javascript in an external js file. Looks like a big ASP project is going on. :) Do you have an outline of the js event? I hate js. :eek: Now is the time that you start loving it. ;)
YES :( I have a large site to develop and I know just enough HTML/CSS to get by but I only did one small static site so far using asp.net. I want to learn it well. :)

The event doesnt seem to be firing :(

Btw, Thanks for the help so far guys. I will be posting many threads over the next week :(

<td style="BORDER-RIGHT: #b22222 1px solid; FONT-SIZE: 12px; BORDER-LEFT: #b22222 1px solid; WIDTH: 140px; FONT-FAMILY: Verdana, Arial; ALIGN: center"
vAlign="top" align="center" bgColor="#eeeeee"><asp:textbox id="txtUsername" BackColor="#cccccc" style="FONT-SIZE: 12px; COLOR: #b22222" runat="server"
Width="110" value="Enter UserName" onFocus="changeVal()></asp:textbox>
<script type="text/javascript" language="JavaScript">
function changeVal() {
if (document.Form1.txtUsername.value == "Enter Username")
document.Form1.txtUsername.value = "";
}
</script>
</td>
Take a look at your code, you are setting the value to Enter UserName and then in the event you are checking for Enter Username
w00t w00t! I'm an ASP.NET programmer now :lol:

It was a stupid missing doublequote from the atuomatic formatting that the ide does. Also found that when it did fire it wasnt matching. I guess its case sensitive?

I didnt know you could place the js code in there that way. :thumb:
One more question...
When I click on the submit button I have it redirect to "#" for testing. When this happens the postback repopulates the login textbox.
Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Response.Redirect("#")
End Sub
Thats because ASP.NET has a different State Management that classic ASP. Read about state management on MSDN.

If you want this should not happen then set EnableViewState of the control to False.
Ok, thanks. I'll look it up. :)
I tried setting it to false in the page_load but if i type in a username and submit, it repopulates it with "Enter UserName" :(
You must be calling a function in the body onload that populates that textbox. Instead of calling the function in body load, send a call to it to the output using Page.RegisterStartupScript.

In your page load event:

If Not Page.IsPostBack
Page.RegisterStartupScript("myscript","nameofthefunction();")
End If
I did this in the page_load event procedure
If Page.IsPostBack = False Then
Me.txtUsername.Text = "Enter UserName"
End If
Me.txtUsername.EnableViewState() = FalseIt appears to work but I wont be 1000% until I finish creating the db and writting the login code.

Does this look correct now? I did have the textbox being populated in the aspx element tag in the html section.
It'll do.
Thanks :)
Actually if you had done your response.redirect to the name of the page "default.aspx" for example, it would not have retained the settings.

When you redirect to an anchor, it instead of redirecting the page just sends the browser a different URL, but maintains state. It does this to help, not hinder, your coding.

Just letting you know that you just killed the proverbial fly with an elephant gun.
Well I like overkill but I just dont have a second page to redirect to yet. I'll test it out with a dummy page.
the second page I speak of is the same page.

Lets say the "test" page was called "test.aspx" then what I was saying is instead of redirecting to "#" you redirect to "test.aspx" (which is the same page).
I'm still new to asp.net but I did redirect to the same page and it didnt retain the login name I used. It repopulated it with the "Enter UserName" again. Is this what you meant?

When it goes to a secondary page it shouldnt be populated at all since they will already be logged in.
Ok, related issue still here. The login txtUserName textbox contents should be added to the User_Name session variable but its not retaining or able to even be placed into a label control. :(

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'Login code
If Session.SessionID Is Nothing Then
'code to alert or redirect to login in again
Response.Redirect("index.aspx")
End If
If Session.Item("sesUser_Name") = Nothing Then
Session.Add("sesUser_Name", "RobDog888")
End If
If Page.IsPostBack = False Then
Me.txtUsername.Text = "Enter UserName"
End If
Me.txtUsername.EnableViewState() = False
End Sub
Maybe you left it out accidentally but it does not look like you are inserting the contents of the sesson variable anyway :)

Something like:

If Session.Item("sesUser_Name") = Nothing Then
Session.Add("sesUser_Name", "RobDog888")
Else
If Page.IsPostBack = False Then
Me.txtUsername.Text = "Enter UserName"
Else
Me.txtUsername.Text = Session.Item("sesUser_Name")
end if
end if

?
Sorry, forgot to post the btnLogin click code. RobDog888 was a test. Like a temp guest user id.
Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If Session.Item("User_Name") Is Nothing Then
Session.Add("User_Name", Me.txtUsername.Text)
End If
Me.lblUserName.Text = Session.Item("User_Name").ToString 'THIS IS NOT POULATING MY LABEL :(
Session.Timeout = 15
'...
End Sub
Btw, thats a really nice looking forums site in your sig. vBull 3.5 :)
is btnLogin your submit button? If so I recommend placing that code in the page load event. Check if you have a username posted and if so add it as a session variable. Elseif check if a session variable is already present and if so place it in the text box.

Then again, I could just be showing my ignorance of ASP.NET. Sorry :(
This is the home page so to be able to login only once and access the secured pages it needs to check on the page load for if they just refresh or renav to the home page after they already logged in. Now if they havent logged in at all then the pageload will not have a txtUserName content to load into the session var. It needs to ignore the "Enter UserName" auto text htat I had populated in the js in the html for the page as shown by Shuja Alis posted code.

So it seems that when the postback happens it doesnt pickup the session var. :(
w00t, got it. It was the stupid Session.SessionID check that was throwing it off. This now retains the uisername (without any verification of login yet) and populates a label control with the username and starts the session timeout of 15 minutes and redirects with SSL. :D
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Session.Item("User_Name") = Nothing Then
Session.Add("User_Name", "Guest")
End If
If Page.IsPostBack = False Then
Me.txtUsername.Text = "Enter UserName"
End If
Me.txtUsername.EnableViewState() = False
End Sub

Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If Session.Item("User_Name") Is Nothing Then
Session.Add("User_Name", Me.txtUsername.Text)
End If
Me.lblUserName.Text = Session.Item("User_Name").ToString
Session.Timeout = 15
Response.Redirect("https://secure.blah.com")
End SubNow I just need to clear the txtUserName box and clear the lblUserName upon page load to take out the control name thats in it by default.

another step closer. :)
Glad to not be of assistance :D
But a second set of eyes is always a help. :) I noticed that when you reposted my code my sesUser_Name var was named differently then it was in the btnLogin clck event. ;)

Now I am multi-tasking and in the middle of creating the users sql table.

Thanks everyone :thumb:
But a second set of eyes is always a help. :) I noticed that when you reposted my code my sesUser_Name var was named differently then it was in the btnLogin clck event. ;)

Now I am multi-tasking and in the middle of creating the users sql table.

Thanks everyone :thumb:
You are still here. :eek:

I though you would have finished your project by now. :)

All the best. :thumb:
Thanks but unfortunately I am a noob at asp.net so its going to take some time. :(

Ps, I have 5 or 6 hours left to finish the entire login and security part. :cry:

0 comments:

Post a Comment