Showing posts with label focus. Show all posts
Showing posts with label focus. Show all posts

Saturday, March 31, 2012

Text Box to change on focus... Possible?

Hi i was just wondering if it was possible to change a text boxes default value onFocus... I have a search bar and rather then put next to it search product here I wanted to have written in the text box as default Enter Product Code.. and then when the box is clicked on it clears this text away and has a blank textbox before they begin to type

Thanks in advance for any help given

Hey,

Yes, there are two ways to do it. There is a TextboxWatermarkExtender (in the AJAXControlToolkit), which renders the text and manages that automatically for you, or you can use manual javascript, simply by catching the focus and when the textbox value is equal to the watermark value, clear it. I don't have an example on-hand, but you should be able to find one.


Create a test ASPX and add following:


<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Text="Enter Here"></asp:TextBox> <br/>
</div>
</form>
<script language="javascript" type="text/javascript">
function ClearValue()
{
document.getElementById("<%=TextBox1.ClientID %>").value = "";
}
</script>


In Code Behind add following:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Attributes.Add("OnFocus", "javascript:ClearValue();")
End Sub

Hope this will serve the purpose.


Code for the text box:

<input id="txtBox1" type="text" value="Some Default Value" onfocus="ClearTxt();" />

Javascript function to clear default value:

<script type="text/javascript">
function ClearTxt() {
document.getElementById("txtBox1").value = ""
}
</script>


you could use a javascript method like teh one shown here

http://www.urban75.org/tech/q024.html


Thanks alot guys for all the help


try this

tst.Attributes.Add("onFocus","ctrl = document.getElementById('tst');ctrl.value = '';")

replace both occurrences of tst with the name of the textbox


Hi sorry i know this is an old post but I was wondering, I managed to get this working the only little thing is if I click in the box it deletes the text (what I was looking for) but if I dont type anything and click away from the box it's still blank.. I was wondering if there was a way to refill the text box with the default text again

Thanks


Hey,

Attach to the client onblur event, that if the textbox's text length is zero, put the text back in the textbox.


I know I am a strong anti-Javascript developer, but using javascript to set properties of a form control is not a very good idea, especially if you consider accessibility issues. Rather, why not to attach labels to the text box, and/or use tool-tip feature for displaying user information. This is both, dynamic, on focus, as well as Accessible to challanged users.

Again, it is kind of personal opinion, but then you have to think about the accessibility issues at some point of time!

Wednesday, March 28, 2012

Text field property

I have a text field i nmy app. If I write "10" in the text field and close the app, then I reload the application and when I focus on the text field I can see all prior entered valued in this text field in a list. I don't like that! How do I prevent this? Is there a property to the textbox which "cleans" the list?i dont know why this would be happening, and im no expert.
i believe it is you own settings in internet explorer

tools
internet options
content
auto complete
:ehh:
This is what I mean:
yes , i believe that is your own setting of internet exploroer.
i.e. if you type in googles search bar, are you greeted with the same drop down list qith ll your previous searches...
Ok, I was not aware of this. Now I have changed the mark in Autocomplete under Forms and everything is ok. But I have to inform all of my users of this :-(
it may be it can be programmed but i am unaware of how,,
perhaps the pragma no cache or something may help,
may be worth a googling if no expert gets back to you here
I don't think this can be changed programmatically. I for one like the feature :D

Thursday, March 22, 2012

textbox

I have an asp.net form with a text box on it and I also have a label
control. What I want to do is when the textbox has focus I want the label
control to be enabled and populate with some tips on what goes in to the
text box. I am using vb.net and I don't see any event that will allow me to
do what I want, and I was wondering if anyone had any ideas on how to do
this. Thanks.

--
J. D.It's a client-side task. A label renders as a <span> and a textbox does as
an <input type=text>.You need to catch the input's onfocus event and set
the span's innerText to the inbox's "title" property.

Eliyahu

"JD" <jdnews@.dalys.us> wrote in message
news:ueb6sxOmFHA.3656@.TK2MSFTNGP09.phx.gbl...
> I have an asp.net form with a text box on it and I also have a label
> control. What I want to do is when the textbox has focus I want the label
> control to be enabled and populate with some tips on what goes in to the
> text box. I am using vb.net and I don't see any event that will allow me
to
> do what I want, and I was wondering if anyone had any ideas on how to do
> this. Thanks.
> --
> J. D.
> I have an asp.net form with a text box on it and I also have a label
> control. What I want to do is when the textbox has focus I want the label
> control to be enabled and populate with some tips on what goes in to the
> text box. I am using vb.net and I don't see any event that will allow me to
> do what I want, and I was wondering if anyone had any ideas on how to do
> this. Thanks.
> --
> J. D.

Hello there,

You must add an 'onchange' attribute programmaticaly:

tb.Attributes.Add("onchange", lbl.UniqueID & ".value='hello there!';");

Antonio
could you give me some code example on the span innertext and is that cross
browser
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:%23Cn1C6OmFHA.1416@.TK2MSFTNGP09.phx.gbl...
> It's a client-side task. A label renders as a <span> and a textbox does as
> an <input type=text>.You need to catch the input's onfocus event and set
> the span's innerText to the inbox's "title" property.
> Eliyahu
> "JD" <jdnews@.dalys.us> wrote in message
> news:ueb6sxOmFHA.3656@.TK2MSFTNGP09.phx.gbl...
>> I have an asp.net form with a text box on it and I also have a label
>> control. What I want to do is when the textbox has focus I want the label
>> control to be enabled and populate with some tips on what goes in to the
>> text box. I am using vb.net and I don't see any event that will allow me
> to
>> do what I want, and I was wondering if anyone had any ideas on how to do
>> this. Thanks.
>>
>> --
>> J. D.
>>
>>
In addition to the solutions posted by Eliyahu and Cactus, you can also
design your customized databound control if the tips for each text box are
saved in a database. This is a sample for such databound control:
http://www.societopia.net/Samples/textBox.aspx

Or you can use client-side scripting to load xml data, as in this example:
http://www.societopia.net/Samples/textBox.htm

Phillip Williams

"JD" wrote:

> I have an asp.net form with a text box on it and I also have a label
> control. What I want to do is when the textbox has focus I want the label
> control to be enabled and populate with some tips on what goes in to the
> text box. I am using vb.net and I don't see any event that will allow me to
> do what I want, and I was wondering if anyone had any ideas on how to do
> this. Thanks.
> --
> J. D.
>

Tuesday, March 13, 2012

textbox

I have an asp.net form with a text box on it and I also have a label
control. What I want to do is when the textbox has focus I want the label
control to be enabled and populate with some tips on what goes in to the
text box. I am using vb.net and I don't see any event that will allow me to
do what I want, and I was wondering if anyone had any ideas on how to do
this. Thanks.
J. D.It's a client-side task. A label renders as a <span> and a textbox does as
an <input type=text>.You need to catch the input's onfocus event and set
the span's innerText to the inbox's "title" property.
Eliyahu
"JD" <jdnews@.dalys.us> wrote in message
news:ueb6sxOmFHA.3656@.TK2MSFTNGP09.phx.gbl...
> I have an asp.net form with a text box on it and I also have a label
> control. What I want to do is when the textbox has focus I want the label
> control to be enabled and populate with some tips on what goes in to the
> text box. I am using vb.net and I don't see any event that will allow me
to
> do what I want, and I was wondering if anyone had any ideas on how to do
> this. Thanks.
> --
> J. D.
>

> I have an asp.net form with a text box on it and I also have a label
> control. What I want to do is when the textbox has focus I want the label
> control to be enabled and populate with some tips on what goes in to the
> text box. I am using vb.net and I don't see any event that will allow me t
o
> do what I want, and I was wondering if anyone had any ideas on how to do
> this. Thanks.
> --
> J. D.
Hello there,
You must add an 'onchange' attribute programmaticaly:
tb.Attributes.Add("onchange", lbl.UniqueID & ".value='hello there!';");
Antonio
could you give me some code example on the span innertext and is that cross
browser
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:%23Cn1C6OmFHA.1416@.TK2MSFTNGP09.phx.gbl...
> It's a client-side task. A label renders as a <span> and a textbox does as
> an <input type=text>.You need to catch the input's onfocus event and set
> the span's innerText to the inbox's "title" property.
> Eliyahu
> "JD" <jdnews@.dalys.us> wrote in message
> news:ueb6sxOmFHA.3656@.TK2MSFTNGP09.phx.gbl...
> to
>
In addition to the solutions posted by Eliyahu and Cactus, you can also
design your customized databound control if the tips for each text box are
saved in a database. This is a sample for such databound control:
http://www.societopia.net/Samples/textBox.aspx
Or you can use client-side scripting to load xml data, as in this example:
http://www.societopia.net/Samples/textBox.htm
Phillip Williams
"JD" wrote:

> I have an asp.net form with a text box on it and I also have a label
> control. What I want to do is when the textbox has focus I want the label
> control to be enabled and populate with some tips on what goes in to the
> text box. I am using vb.net and I don't see any event that will allow me t
o
> do what I want, and I was wondering if anyone had any ideas on how to do
> this. Thanks.
> --
> J. D.
>
>

Textbox and mouse focus

Hi,

I have webform that uses 1 customized user controls(contain textbox1 and textbox2). When the form loads, I want to put the mouse focus on the user controls textbox1. Is this possible?

Thanks in advance!Again in my case I'm trying to position on a textbox1 inside a user control(name.ascx) that is called by home.aspx page.

When I add the below code to the page_load event of home.aspx, it gave the error.

Dim strbuilder As StringBuilder = New StringBuilder

strbuilder.Append("<script language='javascript'>")

strbuilder.Append(" window.document.getElementById('txtkeywords').focus()")

strbuilder.Append("</script>")

RegisterStartupScript("Focus", strbuilder.ToString)

Error:
Microsoft JScript runtime error: 'window.document.getElementById(...)' is null or not an object

Any help is grealty appreciated
Say that your user control has the ID of "myUserControl".

You would then change your code from:

 strbuilder.Append(" window.document.getElementById('txtkeywords').focus()")
to
 strbuilder.Append(" window.document.getElementById('" & myUserContro.FindControl('txtkeywords').ClientID & "').focus()")

Admittedly, I haven't tested this ... but it should work.
I replaced the code you gave me, but it gave me a Expression Expected error, indicated in between getElementbyID and (. Since I don't know Javascript well, I remove the two "s from your code and it gave me runtime error: Expected ')' on this line of code. How do I make this work?

Thanks for you help.
My apologies, I should have tested the code before I posted it.

Here is the user control:

 <%@. Control Language="VB" %>
<script runat="server"
Function GetTextboxID() As String
Return txtkeywords.ClientID
End Function

</script>
<p>I am the UserControl</p>
<p>Keywords: <asp:Textbox id="txtkeywords" runat="server" /></p>


The only interesting thing is that I've given the User Control a method called GetTextboxID. This simply returns the ClientID property of its Textbox control. You see, when the page finally renders, the ID of this textbox will no longer be "txtkeywords". It will be the ID of the user controlplus the ID of the textbox, separated by an underscore: myUserControl_txtkeywords.

This is why your code was never working ... the Javascript was looking for a control with the ID txtkeywords, when what it should be looking for was the ID myUserControl_txtkeywords.

And we've now given our User Control a method that returns this extended ID (known as the ClientID).

So, we can now update our ASPX Page so that it calls this function when creating the focus Javascript function:

 <%@. Page Language="VB" %>
<%@. Register TagPrefix="my" TagName="UserControl" src="http://pics.10026.com/?src=mycontrol15.ascx" %>
<script runat="server"
Sub Page_Load( sender As Object, eArgs As EventArgs )
Dim focusScript As Text.StringBuilder = New Text.StringBuilder
focusScript.Append("<scr" & "ipt language=""javascript"" type=""text/javascript"">")
focusScript.Append("window.document.getElementById('" &myUserControl.GetTextboxID() & "').focus()")
focusScript.Append("</scr" & "ipt>")
RegisterStartupScript("FocusScript", focusScript.ToString)
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>Page Header</h3>
<hr />
<my:UserControl id="myUserControl" runat="server" />
<hr />
<h4>Page Footer</h4>
</form>
</body>
</html>

Does that work for you?
Thanks for your help! But, I created a usercontrol and a form and copied your code. When I run it, it doesn't place the focus on textbox. what went wrong?

here are my code:
myControl.ascx


<%@. Control Language="vb" AutoEventWireup="false" Codebehind="myControl.ascx.vb" Inherits="SettingFocus.myControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<script runat="server"
Function GetTextboxID() As String

Return txtkeywords.ClientID

End Function

</script>
<p>I am the UserControl</p>
<p>Keywords:
<asp:Textbox id="txtkeywords" runat="server" /></p

And the form file DefaultFocus.aspx


<%@. Register TagPrefix="my" TagName="UserControl" src="http://pics.10026.com/?src=myControl.ascx" %>
<%@. Page Language="vb" AutoEventWireup="false" Codebehind="DefaultFocus.aspx.vb" Inherits="SettingFocus.DefaultFocus"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<script runat="server"
Sub Page_Load( sender As Object, eArgs As EventArgs )

Dim focusScript As Text.StringBuilder = New Text.StringBuilder

focusScript.Append("<scr" & "ipt language=""javascript"" type=""text/javascript"">")

focusScript.Append("window.document.getElementById('" & myUserControl.GetTextboxID() & "').focus()")

focusScript.Append("</scr" & "ipt>")

RegisterStartupScript("FocusScript", focusScript.ToString)

End Sub

</script
<html
<head
</head
<body
<form runat="server" ID="Form1"
<h3>Page Header</h3
<hr /
<my:UserControl id="myUserControl" runat="server" /
<hr /
<h4>Page Footer</h4
</form
</body
</html>


We'll need to update our code slightly as you're using the codebehind method.

Here is our Control.ascx user control. I've now removed the inline code:

 <%@. Control AutoEventWireup="False" Inherits="Test.ControlCodebehind" %>
<p>I am the UserControl</p>
<p>Keywords: <asp:Textbox id="txtkeywords" runat="server" /></p>

The code has now moved into the codebehind:

 Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Test

Public Class ControlCodebehind
Inherits System.Web.UI.UserControl

Protected txtkeywords As Textbox

Function GetTextboxID() As String
Return txtkeywords.ClientID
End Function

End Class

End Namespace

Our page.aspx also has its inline code removed:

 <%@. Page AutoEventWireup="False" Inherits="Test.PageCodebehind" %>
<%@. Register TagPrefix="my" TagName="UserControl" src="http://pics.10026.com/?src=control.ascx" %>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>Page Header</h3>
<hr />
<my:UserControl id="myUserControl" runat="server" />
<hr />
<h4>Page Footer</h4>
</form>
</body>
</html>

And the code is moved into its codebehind. I've highlighted the only real change:

 Imports System
Imports System.Text
Imports System.Web.UI

Namespace Test

Public Class PageCodebehind
Inherits System.Web.UI.Page

Protected myUserControl As Test.ControlCodebehind

Protected Sub Page_Load( ByVal sender As Object, ByVal eArgs As System.EventArgs ) Handles MyBase.Load

Dim focusScript As StringBuilder = New StringBuilder
focusScript.Append("<scr" & "ipt language=""javascript"" type=""text/javascript"">")
focusScript.Append("window.document.getElementById('" & myUserControl.GetTextboxID() & "').focus()")
focusScript.Append("</scr" & "ipt>")
RegisterStartupScript("FocusScript", focusScript.ToString)

End Sub

End Class

End Namespace


As you can see, the only real change is that we've declared our myUserControl not as a UserControl, but as the class name for our user control's codebehind.

Does that help?
NO Luck. I'm getting the same result. I'm wondering if the code works on your pc. If it does then there must be something else other than the source code that is causing the problem. I'm running Visual Studio .NET 2003, with framework 1.1 and IIS 5.
> I'm wondering if the code works on your pc.

It sure does, because I made a test app called DotNetBeginner in IIS, with the code shown. (I too use .NET 1.1 on IIS 5.)

You can download this test app:
www.edition3.com/developers/DotNetBeginner/code.zip

Create a new folder within \wwwroot and unzip the files. Use IIS to configure this new folder as an app. And then request page.aspx.

Let me know if that works.
Not to step on SomeNewKid's efforts, I'm sure his code will work, I haven't tested it, but when I want to simply set focus on a textbox, this is what I do...

Let's say my textbox name is txtEntry.
In the page_load event, I place this code.


Me.SetFocus(Me.txtEntry)

Then, elsewhere, I create this...

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"
RegisterStartupScript("focus", s)
End Sub

And that's it. You can put the Me.SetFocus(Me.txtEntry) anywhere on your page as well.

Hope this helps as well,

Zath
Hi.. how about if i want to put the coding in a .cs file? i found that it have an error on this line

RegisterStartupScript("focus", s)?

The name 'RegisterStartupScript' does not exist in the class or namespace 'Utilities.HandleUtil.HandlerUtil'

What wrong actually?

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)

Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"

RegisterStartupScript("focus", s)

End Sub
RegisterStartupScript is a method of System.Web.UI.Page.

So RegisterStartupScript really needs to be used within your codebehind ... not within a Utility class.

Although, if you like, the Utility class can generate the script, but the codebehind actually places it on the Page with RegisterStartupScript.
Hi SomeNewKid,

This maybe a dumb question, but since I'm a beginner, please don't mind me asking. How do configure the new folder as an application in IIS?

Thanks
Gwen
Open the Internet Services Manager (under Administrative tools) expand
your website and right-click on webserver, select all tasks->New virtual directory,
give it a name, browse to the direcotry that contains the files, and voila
you have configured your application!

Regards
Fredr!k
I do it aslightly different way to Fredrick2000.

To make it easy, I've created a little tutorial with screenshots, starting here:
www.edition3.com/developers/DotNetBeginner/step1.htm

I hope this helps.

textbox and the tab key

What's the best way to prevent the tab key from shifting focus away from a
textbox, but rather insert the tab character? Do I just do it the obvious
way with JavaScript or is there some simple property to set?
PaulPJ6 wrote:
> What's the best way to prevent the tab key from shifting focus away from a
> textbox, but rather insert the tab character? Do I just do it the obvious
> way with JavaScript or is there some simple property to set?
> Paul
I'd simply change the textbox to a "multiple line" textbox also known
as the textarea in HTML forms. You can have a textarea in same height
as a textbox so the design doesn't get crushed by it. The textarea
supports the tab character and you're done :)
-Kevin
Um... already have a multiple line textarea. Tab still alters focus. Is it
just an IE problem?
Paul
"Kevin Steffer [MCP]" <ksteffer@.gmail.com> wrote in message
news:1133899279.462906.116050@.g44g2000cwa.googlegroups.com...
> PJ6 wrote:
> I'd simply change the textbox to a "multiple line" textbox also known
> as the textarea in HTML forms. You can have a textarea in same height
> as a textbox so the design doesn't get crushed by it. The textarea
> supports the tab character and you're done :)
> -Kevin
>
the <textarea> doesn't support tab entry (other than a paste). you can use
client script to do it though, through the onkeydown event. if the key is a
tab, insert the tab char into the current postion..
-- bruce (sqlwork.com)
"PJ6" <nobody@.nowhere.net> wrote in message
news:O1Gp%234p%23FHA.2452@.TK2MSFTNGP11.phx.gbl...
> What's the best way to prevent the tab key from shifting focus away from a
> textbox, but rather insert the tab character? Do I just do it the obvious
> way with JavaScript or is there some simple property to set?
> Paul
>
Yeah, that's what I thought I had to do :-/
Thanks,
Paul
"Bruce Barker" <brubar_nospamplease_@.safeco.com> wrote in message
news:OcREbjq%23FHA.4092@.TK2MSFTNGP10.phx.gbl...
> the <textarea> doesn't support tab entry (other than a paste). you can use
> client script to do it though, through the onkeydown event. if the key is
> a tab, insert the tab char into the current postion..
> -- bruce (sqlwork.com)
>
> "PJ6" <nobody@.nowhere.net> wrote in message
> news:O1Gp%234p%23FHA.2452@.TK2MSFTNGP11.phx.gbl...
>