How to add onfocus to my textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • readbanana
    New Member
    • Jul 2010
    • 37

    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
    Code:
     TextBox tb = (TextBox)e.Row.FindControl("costTB");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              tb.Attributes.Add("onfocus", "Visible(this)");
              tb.Attributes.Add("onblur", "Invisible(this)");
            }
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    what javascript framework do you use? some ASP.net addOn or similar? ... the code you posted is far from any standard JavaScript.

    Comment

    • readbanana
      New Member
      • Jul 2010
      • 37

      #3
      I'm working in asp.net - C#. Like I said, I don't know any javascript. All those functions do is document.getele mntbyid('leftDi v').style.visib ility='visible'
      If you have something else that would work right, I'd love to hear.

      Comment

      • readbanana
        New Member
        • Jul 2010
        • 37

        #4
        I just realized I posted what was on my .cs page, not the javascript function - that's just calling the function.
        Code:
        function Visible(obj)
        {
        document.getElementById("leftDiv").style.visibility='visible';
        document.getElementById("rightDiv").style.visibility='visible';
        
        }
        function Invisible(obj)
        {
        document.getElementById("leftDiv").style.visibility='hidden';
        document.getElementById("rightDiv").style.visibility='hidden';
        }

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          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

          Comment

          • readbanana
            New Member
            • Jul 2010
            • 37

            #6
            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.

            Code:
               TextBox tb = (TextBox)e.Row.FindControl("costTB");
                    if (tb != null)
                    {
                        tb.Attributes.Add("onfocus", "Visible(this)");
                        tb.Attributes.Add("onblur", "Invisible(this)");
                    }

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              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 ...

              Comment

              • readbanana
                New Member
                • Jul 2010
                • 37

                #8
                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?

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  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 ...

                  Comment

                  • readbanana
                    New Member
                    • Jul 2010
                    • 37

                    #10
                    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!

                    Code:
                     <ItemTemplate>
                    
                                 <div id="leftDiv" class="leftdiv" style="display:none"></div>
                                   <asp:TextBox ID="costTB" runat="server" Text='<%# Bind("Cost") %>' AutoPostBack="true" CssClass="grid-input"  ></asp:TextBox>
                                   <div id="rightDiv" class="rightdiv" style="display:none"></div>
                     
                                </ItemTemplate>

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      how is the serverside code rendered in the client - i mean how does the resulting html ook like?

                      Comment

                      • readbanana
                        New Member
                        • Jul 2010
                        • 37

                        #12
                        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

                        Comment

                        • readbanana
                          New Member
                          • Jul 2010
                          • 37

                          #13
                          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

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5388

                            #14
                            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.

                            Comment

                            • readbanana
                              New Member
                              • Jul 2010
                              • 37

                              #15
                              this is from firebug. you can't view source while your focused - it takes the focus off
                              Code:
                              <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">

                              Comment

                              Working...