473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to add onfocus to my textbox

I am trying to add onfocus to my textbox that is in a gridview. No matter which textbox I click in that row, the divs become visible in the first cell only
Expand|Select|Wrap|Line Numbers
  1.  TextBox tb = (TextBox)e.Row.FindControl("costTB");
  2.         if (e.Row.RowType == DataControlRowType.DataRow)
  3.         {
  4.           tb.Attributes.Add("onfocus", "Visible(this)");
  5.           tb.Attributes.Add("onblur", "Invisible(this)");
  6.         }
Jul 21 '10 #1
25 4285
gits
5,390 Expert Mod 4TB
what javascript framework do you use? some ASP.net addOn or similar? ... the code you posted is far from any standard JavaScript.
Jul 21 '10 #2
I'm working in asp.net - C#. Like I said, I don't know any javascript. All those functions do is document.getelemntbyid('leftDiv').style.visibility ='visible'
If you have something else that would work right, I'd love to hear.
Jul 21 '10 #3
I just realized I posted what was on my .cs page, not the javascript function - that's just calling the function.
Expand|Select|Wrap|Line Numbers
  1. function Visible(obj)
  2. {
  3. document.getElementById("leftDiv").style.visibility='visible';
  4. document.getElementById("rightDiv").style.visibility='visible';
  5.  
  6. }
  7. function Invisible(obj)
  8. {
  9. document.getElementById("leftDiv").style.visibility='hidden';
  10. document.getElementById("rightDiv").style.visibility='hidden';
  11. }
Jul 21 '10 #4
gits
5,390 Expert Mod 4TB
what is the exact problem then? ... the JavaScript code would set the visibility css-property but first you stated that you want to apply an onfocus method to a textbox?

what obj is passed to the two functions you posted? from where are they called?

kind regards
Jul 22 '10 #5
obj should be the textbox you click. On focus of a textbox in a gridview, I want the divs surrounding that textbox to become visible.

Expand|Select|Wrap|Line Numbers
  1.    TextBox tb = (TextBox)e.Row.FindControl("costTB");
  2.         if (tb != null)
  3.         {
  4.             tb.Attributes.Add("onfocus", "Visible(this)");
  5.             tb.Attributes.Add("onblur", "Invisible(this)");
  6.         }
Jul 22 '10 #6
gits
5,390 Expert Mod 4TB
so in that case it seems that your code relies on ids that wouldn't be unique. id's have to be unique otherwise getElementById() fails ...
Jul 22 '10 #7
I have the same thing for a hover over an image and that works - the right image shows. do you have a better way that I can go about doing this?
Jul 22 '10 #8
gits
5,390 Expert Mod 4TB
so do you have nodes with duplicate ids? if not try setting the display property to 'none' (=false) or 'block' instead of using the visibility ...

btw. it is a bit difficult to deal with problems when having only small pieces of information ...
Jul 22 '10 #9
I don't have separate nodes. it's a databound column. I've tried setting the display - but it's exactly the same. Tell me what information you need and I'll give it to you. I need this done quickly! I'm getting pretty desperate!

Expand|Select|Wrap|Line Numbers
  1.  <ItemTemplate>
  2.  
  3.              <div id="leftDiv" class="leftdiv" style="display:none"></div>
  4.                <asp:TextBox ID="costTB" runat="server" Text='<%# Bind("Cost") %>' AutoPostBack="true" CssClass="grid-input"  ></asp:TextBox>
  5.                <div id="rightDiv" class="rightdiv" style="display:none"></div>
  6.  
  7.             </ItemTemplate>
Jul 22 '10 #10
gits
5,390 Expert Mod 4TB
how is the serverside code rendered in the client - i mean how does the resulting html ook like?
Jul 22 '10 #11
the two divs show a yellow little line on each side the background of the textbox becomes yellow. when you click the second row, the background becomes yellow (because thats the css :hover) but the two divs stay visible in the firsts cell. if you click a different column then the divs go away since you are no longer on focus. I'm not sure if I understood your question and gave the proper answer
Jul 22 '10 #12
I really understand what the problem is I just need to know how to get around it. really I need to say e.row.cellp[2].attributes.add("onfocus") - you just can't add onfocus to a cell
Jul 22 '10 #13
gits
5,390 Expert Mod 4TB
i meant the html-code - could you post a portion of the page when it is loaded in the browser and you have a look at the source view? and it should of course be a piece where the nodes with the divs in question appear.
Jul 22 '10 #14
this is from firebug. you can't view source while your focused - it takes the focus off
Expand|Select|Wrap|Line Numbers
  1. <input type="text" onblur="Invisible(this)" onfocus="Visible(this)" class="grid-input" id="ctl00_ContentPlaceHolder1_gridUC_summaryGV_ctl03_costTB" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$gridUC$summaryGV$ctl03$costTB\',\'\')', 0)" value="294.00" name="ctl00$ContentPlaceHolder1$gridUC$summaryGV$ctl03$costTB">
Jul 22 '10 #15
gits
5,390 Expert Mod 4TB
the textbox isn't very relevant in this case ... we would need to have a look at the surrounding code ... i think the problem is the setting of the display of the divs? i think that such a line:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById("leftDiv").style.visibility='visible';
fails ... and i suppose that "leftDiv" is either not unique or that id is not present at all ... so i asked for the html-code where the divs appear.
Jul 22 '10 #16
Expand|Select|Wrap|Line Numbers
  1. <div style="display: none;" class="leftdiv" id="leftDiv"></div><input type="text" onblur="Invisible(this)" onfocus="Visible(this)" class="grid-input" id="ctl00_ContentPlaceHolder1_gridUC_summaryGV_ctl03_costTB" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$gridUC$summaryGV$ctl03$costTB\',\'\')', 0)" value="294.00" name="ctl00$ContentPlaceHolder1$gridUC$summaryGV$ctl03$costTB"><div style="display: none;" class="rightdiv" id="rightDiv"></div>
Jul 22 '10 #17
each <td> has the divs surrounding them but they stay display:none. the first divs change to display block
Jul 22 '10 #18
gits
5,390 Expert Mod 4TB
so the id's aren't unique as a already metioned. you should make them unique by adding something unique to them serverside which could be recognized from your textbox - for example its id - so the function on the client could look like this then:

Expand|Select|Wrap|Line Numbers
  1. function Visible(obj) {
  2.     var lDivId = 'leftDiv_' + obj.id;
  3.     document.getElementById(lDivId).style.visibility='visible';
  4. }
for example.
Jul 22 '10 #19
What would the server side code look like? This doesn't work just like this
Jul 26 '10 #20
gits
5,390 Expert Mod 4TB
i guess the ItemTemplate would need to take care of that - there the div's ids are assigned -> so they would need to be adapted.
Jul 26 '10 #21
I'm not exactly sure what you are saying I should do. The code that you gave me doesn't do anything - my divs don't show up at all.
Jul 26 '10 #22
gits
5,390 Expert Mod 4TB
  • the id of a node has to be unique in a document - that is the purpose of an id
  • in your serverside code make it unique (this is probably to do in the ItemTemplate that you posted)
  • the id should be identifyable by the node that is passed to the Visible() method for example its id - so that the method could then identify the corresponding divs - note that this id in the current shown code would need to be the clientId of the textnode
Jul 26 '10 #23
I thought that's what (this) passes in.it tells it which textbox it is. how would I know they dynamically given id?
Jul 26 '10 #24
gits
5,390 Expert Mod 4TB
don't know asp.net quite well - but i think there is a property called ClientID for a control - which should be the one you might use ...

PS: and this is then the object=textbox-node ... but you would need to use a property like the id then ... as shown already
Jul 26 '10 #25
I figured it out.I have div, textbox, div. so the div is a sibling of the textbox. Instead of saying leftdiv visible, I made the previous and next siblings of the textbox invisible. Thanx for all your help.

Expand|Select|Wrap|Line Numbers
  1. function Visible(textbox)
  2. {
  3. var rightdiv =textbox.nextElementSibling;
  4. rightdiv.style.display='block';
  5. var leftdiv = textbox.previousElementSibling;
  6. leftdiv.style.display= 'block';
  7. }
  8. function Invisible(textbox)
  9. {
  10. var rightdiv =textbox.nextElementSibling;
  11. rightdiv.style.display='none';
  12. var leftdiv = textbox.previousElementSibling;
  13. leftdiv.style.display= 'none';
  14. }
Jul 27 '10 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike Berger | last post by:
So, I'm fairly new to this and my code is as follows... <script language="JavaScript> var oldValue function focusElement(theElement) { oldValue = theElement.value; return; } </script>
1
by: Rich Morey | last post by:
Hello, I have created a form and assigned each of the form elements to a class in a style sheet. I would like to know if it is possible to add 'OnFocus' and 'OnBlur' events to the style sheet...
1
by: Michael Albanese | last post by:
I am developing an application to handle my compay's OSHA reporting requirements. Some of the input criteria are technical and narowly defined, so I was trying to prvide what i call "Context...
2
by: Paresh Shah | last post by:
Hi Friends... I have an query on how to capture onFocus event for all the input controls without writing onFocus event in the <input> tag. say my html form has 3 or 4 or 5 input text boxes,...
2
by: swathi | last post by:
----------------------------------------------------------------------- A poll associated with this post was created, to vote and see th results, please visit...
1
by: Tzachi | last post by:
Hello all, I have a function that dynamically adds rows and columns to the page. Everything works well except onfocus // onblur attributes. For some reason, when entering the input box it...
1
by: Eric Trav | last post by:
Hello, I am using mozilla and javascript to change the style background color for my select with onfocus() and back to white with onblur(). When i process onfocus(); i have to click on the...
1
by: clemtig | last post by:
Say I have a form with two input textfields. Each has an onFocus and onBlur. onFocus will unhide image #1. onBlur will hide image #1, show image #2 and validate the textfield. But the problem is that...
4
by: spree | last post by:
Hi, I have a coloumn of buttons where i would like to highlight it when focussed... So, i have used onFocus element for it. Till here its ok. But if i set any background color for the button and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.