How do you Highlight text in input control when it has the focus

Hi All,

My question is How do you Highlight text in an input control when you tab to the control?

Thanks In Advance
0
HallieBerry
12/30/2008 2:24:15 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

3 Replies
351 Views

Similar Articles

[PageSpeed] 2

Try:

this.TextBox1.Attributes.Add("onfocus", "javascript:document.getElementById('" + this.TextBox1.ClientID + "').select();");

this.TextBox2.Attributes.Add("onfocus", "javascript:document.getElementById('" + this.TextBox2.ClientID + "').select();");

this.TextBox3.Attributes.Add("onfocus", "javascript:document.getElementById('" + this.TextBox3.ClientID + "').select();");


"Object reference not set to an instance of an object"
0
che3358
12/30/2008 3:12:39 PM

This is an interesting issue which can be solved easily or hard dependending on the situation. If your TextBox background is white then this is pretty straight forward but if you are attaching the inline style with your TextBox then this is little hard to do. Let' check out the following code: 

Here is the style for the ASP.NET page:

  <style type="text/css">
   
    .highlight { background-color:Yellow; }
   
    .default { background-color:White; }
   
    </style>

<form id="form1" runat="server">
    <div>
   
    <asp:TextBox ID="txtName" runat="server" onBlur='removeHighlight(this)' onFocus="addHighlight(this)" />
   
    </div>
    </form>

And here is the JavaScript object: 

 <script language="javascript" type="text/javascript">

var controlInlineBackgroundColor = '';

function isTextBoxControl(control)
{
    return (control.tagName == 'INPUT' && control.type == 'text');
}

function containsEmbeddedStyle(control)
{
    return control.style.length > 0;
}

function addHighlight(control)
{
    if(containsEmbeddedStyle(control))
    {   
        controlInlineBackgroundColor = control.style.backgroundColor;
        control.removeAttribute("style");       
    }

    if(isTextBoxControl(control))
    {        
         control.className = 'highlight';      
    }
}

function removeHighlight(control)
{
    if(controlInlineBackgroundColor.length > 0)
    {
       control.style.backgroundColor = controlInlineBackgroundColor;
       return;
    }

    if(isTextBoxControl(control))
    {        
         control.className = 'default';  
    }
}

</script>

This seems to be a lot of code for simple TextBox background color but this will also solve the inline style problem. 

Let me know if this helps! 

 

 

 


HighOnCoding
Wanna get high!
0
azamsharp
12/30/2008 5:55:59 PM

Maybe I was wrong. Did he want to change input control's BACKGROUND or just HIGHLIGHT the text in the control?


"Object reference not set to an instance of an object"
0
che3358
12/30/2008 6:06:49 PM
Reply:

Similar Artilces:

superreview granted: [Bug 269318] TAB doesn't start from selection text after mouse click : [Attachment 166255] If element gets focused, move selection to focus if focus isn't already inside the sel
neil@parkwaycc.co.uk <neil.parkwaycc.co.uk@myrealbox.com> has granted Aaron Leventhal <aaronleventhal@moonset.net>'s request for superreview: Bug 269318: TAB doesn't start from selection text after mouse click https://bugzilla.mozilla.org/show_bug.cgi?id=269318 Attachment 166255: If element gets focused, move selection to focus if focus isn't already inside the selection. mLastFocusedWith is no longer needed for making tab not move relative to selection after a mouse click https://bugzilla.mozilla.org/attachment.cgi?id=166255&action=edit ------- Addition...

superreview requested: [Bug 269318] TAB doesn't start from selection text after mouse click : [Attachment 166255] If element gets focused, move selection to focus if focus isn't already inside the s
Aaron Leventhal <aaronleventhal@moonset.net> has asked neil@parkwaycc.co.uk <neil.parkwaycc.co.uk@myrealbox.com> for superreview: Bug 269318: TAB doesn't start from selection text after mouse click https://bugzilla.mozilla.org/show_bug.cgi?id=269318 Attachment 166255: If element gets focused, move selection to focus if focus isn't already inside the selection. mLastFocusedWith is no longer needed for making tab not move relative to selection after a mouse click https://bugzilla.mozilla.org/attachment.cgi?id=166255&action=edit ...

preserving the layout of text input via text input control
Ok what I want to do is, when using the text input control all cr, lf, crlf and spaces are preserved when the content is input in a db and that the information/layout is preserved when viewing it in a text input filed or grid control.any ideas   jim When you input text via a multiline textbox and insert it into a database your specified line breaks will be maintained. When retrieving the record, you will need to do a replace on the string in order to interpret these line breaks in your html:myField.Replace("\n", "<br />")Does this answer your q...

How to populate other text controls by losing focus of one text control
 Hi All,Iam new to ASP.net. Here is a small illustration about my prob. I have 3 text controls in a page i.e which represents zip,city,state. I want to populate city and state after entering zipcode into zip text control i.e something like focus lost event on zip text control. Any solution regarding this thanks and regards,vijaycanaan How about OnTextChange: http://msdn.microsoft.com/en-us/library/aa710190(VS.71).aspx EricPascarello.com | Twitter epascarello | LinkedIn Hi vijaycanaan We can capture the following event when TextBox  lost focus: On Server-Side:We...

Fix broken text wrapping and control input text length in TextBox control
Hi! I'm developing a(nother; it's my second!) ASP.NET application that right now is giving me fits. On one of my screens, the user needs to be able to enter large amounts of text into three database fields. The database fields are all char(350). I'm having two problems with this. First of all, I found that when editing an existing record, text in the fields would wrap in a manner similar to this: Wow, what a sorryperformance! The text box is much wider than the text "Wow, what a sorry", so I couldn't figure out why the text was wrapping. I tried setting the Columns property to 90 (which...

superreview requested: [Bug 394792] Only bother updating text input commands for focused text inputs
Boris Zbarsky (gone 9/4-9/11 and 9/14-9/17) <bzbarsky@mit.edu> has asked neil@parkwaycc.co.uk <neil@httl.net> for superreview: Bug 394792: Only bother updating text input commands for focused text inputs https://bugzilla.mozilla.org/show_bug.cgi?id=394792 Attachment 280807: Like so https://bugzilla.mozilla.org/attachment.cgi?id=280807&action=edit ------- Additional Comments from Boris Zbarsky (gone 9/4-9/11 and 9/14-9/17) <bzbarsky@mit.edu> It's not an issue for undo, because we're not an editor listener unless we're focused. ...

How do I get focus control between 2 forms and a text field
I have a focus problem that maybe someone can help with. I have 2 forms separated by a split bar. The whole left form is used to display an image using calls to a DLL. The right form has a text fields for the title of the image. When you start the program, the text field has focus and the cursor is blinking on the text field. I'm trying to use the DELETE, +/- (for zoom) and CURSOR KEYS on the left form. Is there an WINAPI call that will allow me to give focus to the left form? Many thanks. Harry You description is hard to understand. What do you mean 2 forms separated ...

superreview granted: [Bug 394792] [FIX] Only bother updating text input commands for focused text inputs
neil@parkwaycc.co.uk <neil@httl.net> has granted Boris Zbarsky (gone 9/4-9/11 and 9/14-9/17) <bzbarsky@mit.edu>'s request for superreview: Bug 394792: [FIX]Only bother updating text input commands for focused text inputs https://bugzilla.mozilla.org/show_bug.cgi?id=394792 Attachment 280807: Like so https://bugzilla.mozilla.org/attachment.cgi?id=280807&action=edit ...

Changing the text of an input (text) control
Hi everyone, please answer the following: I have a html input control as follows  <input id="newSearch_keywords" type="text" class="text" value="<%=JobsSearchBusinessObject.CurrentKeywords %>" name="newSearch_keywords""/>    When the page loads I need to look at the text of this input field and remove any * chars that are in the text. Thanks for your help <input id="newSearch_keywords" type="text" class="text" value="<%=JobsSearchBusinessObject.CurrentKeywords.Re...

superreview granted: [Bug 394792] [FIX] Only bother updating text input commands for focused text inputs #2
Robert O'Callahan <roc@ocallahan.org> has granted superreview: Bug 394792: [FIX]Only bother updating text input commands for focused text inputs https://bugzilla.mozilla.org/show_bug.cgi?id=394792 Attachment 280949: With that approach https://bugzilla.mozilla.org/attachment.cgi?id=280949&action=edit ...

superreview granted: [Bug 241942] Parent window should also get focus when input textbox gets focus : [Attachment 148769] patch
Johnny Stenback <jst@mozilla.jstenback.com> has granted Brian Ryner (IBM) <bryner@brianryner.com>'s request for superreview: Bug 241942: Parent window should also get focus when input textbox gets focus http://bugzilla.mozilla.org/show_bug.cgi?id=241942 Attachment 148769: patch http://bugzilla.mozilla.org/attachment.cgi?id=148769&action=edit ------- Additional Comments from Johnny Stenback <jst@mozilla.jstenback.com> r+sr=jst ...

superreview requested: [Bug 241942] Parent window should also get focus when input textbox gets focus : [Attachment 148769] patch
Brian Ryner (IBM) <bryner@brianryner.com> has asked Johnny Stenback <jst@mozilla.jstenback.com> for superreview: Bug 241942: Parent window should also get focus when input textbox gets focus http://bugzilla.mozilla.org/show_bug.cgi?id=241942 Attachment 148769: patch http://bugzilla.mozilla.org/attachment.cgi?id=148769&action=edit ...

events triggered when a data input control such as text box or drop down list receives focus
I need to display a help message for each of the data input controls (text box drop down list for example) in a web form when the control receives focus, the message will be displayed at a message box. I can't find an event handler for that for text box, is there a way to achieve this? By the way, we take aside the use of tooltip at this moment      Hey, this would be a javascript event not a server side event. By default you only get the OnClientClick to handle javascript clicks but you can add any event that you can find in the javascript manual by ma...

unable to input into the input type=text control
i am dynamically creating some controls on the form using javascript in which i have an input control which is text box but when i try to input some thing it is not taking anything what could be wrong. I found that i am populating a Message window n if it is poped the input control is not accepting anything n when it is off it is taking input.  the code for message window is as below can somebody find it out what is wrong in the below code which is preventing me to input text in the input control when it is populated.   MessageWindow = new Window('MessageWindowId'...

Web resources about - How do you Highlight text in input control when it has the focus - asp.net.getting-started

Magic Tour Highlights - Wikipedia, the free encyclopedia
Magic Tour Highlights is an EP by Bruce Springsteen and the E Street Band , which consists of four live audio tracks and their accompanying videos, ...

Facebook Highlights GOP-Flavored Apps Over Drinks At Republican Convention
The six hottest political apps for Facebook were the talk of Tampa, Fla. today during the social network’s cocktail reception they’re calling ...

UFC 194: Video of Conor McGregor knock out against Jose Aldo - UFC, Main Event, UFC Video, UFC Highlights ...
CONOR McGregor barely broke a sweat in the featherweight title fight at UFC 194, ending the superstar bout with Jose Aldo in just 13 seconds. ...

Liverpool v West Brom goals video; Highlights of 2-2 EPL draw at Anfield; Divock Origi, Jordan Henderson ...
DIVOCK Origi scored his first Premier League goal in the 96th minute to earn Liverpool a 2-2 draw in a heated match against West Bromwich Albion ...

Paris climate deal: Highlights of the COP21 accord to limited global warming
Envoys from 195 nations have approved a historic pact to roll back global warming and shore up defences against its impacts.


Asylum row highlights Thai human trafficking
Police weigh defamation case against investigator who fled to Australia and has implicated senior security officials.

Banksy paints Steve Jobs portrait to highlight Syrian refugee crisis
Banksy, the legendary graffiti and street artist whose identity still remains something of a mystery, recently paid a visit to the "Jungle" Refugee ...

Army vs. Navy: Live Score and Highlights - Bleacher Report
Latest: That's going to wrap up our live coverage of the 2015 Army-Navy Game. Thanks for joining us, and be sure to check back for all of your ...

Highlights: Warriors overcome injuries, fight to stay perfect
... Login Search form Search 51° Navigation Home Giants A's Sharks Warriors Kings 49ers Raiders Quakes Insiders More Tickets Shop Watch Highlights: ...

Resources last updated: 12/16/2015 9:17:37 AM