Problem with OnTextChanged event (Newbie)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • progman417
    New Member
    • Mar 2008
    • 13

    Problem with OnTextChanged event (Newbie)

    I'm building my first little application in .net (C# - framework 3.5).

    So far, I've built a search page where the user enters values into text boxes, clicks a button and it displays a grid of results. I'm using an updatepanel to prevent full postbacks. All ok so far!

    When the user changes a value in one of the text boxes, I'm using the OnTextChanged event to hide the grid panel until they hit the button to view.

    It sort of works, but when the user clicks the button, the OnTextChanged event fires and the button does nothing, so a second click is required.

    In an ideal world, what I'd like is for the client side OnKeyPress event to hide the panel as soon as the user starts typing. I found a code example that will perform a postback from Javascript, the problem is that it took focus away from the textbox.

    Any ideas? Go easy I'm new to .net!
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by progman417
    I'm building my first little application in .net (C# - framework 3.5).

    So far, I've built a search page where the user enters values into text boxes, clicks a button and it displays a grid of results. I'm using an updatepanel to prevent full postbacks. All ok so far!

    When the user changes a value in one of the text boxes, I'm using the OnTextChanged event to hide the grid panel until they hit the button to view.

    It sort of works, but when the user clicks the button, the OnTextChanged event fires and the button does nothing, so a second click is required.

    In an ideal world, what I'd like is for the client side OnKeyPress event to hide the panel as soon as the user starts typing. I found a code example that will perform a postback from Javascript, the problem is that it took focus away from the textbox.

    Any ideas? Go easy I'm new to .net!
    If you want to hide the grid on the first keypress until the user clicks the button, then in you textChanged event, check for the length of the text box. If it's zero, then disable the grid. Otherwise, just let it be.

    Perhaps an even better way would be to handle the GotFocus event of the textbox.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Um... don't want to scare you but this I would say is a classic scenario for using the AJAX extenders (from an ASP.NET standpoint). The AJAX extenders write the JavaScript for you, so you don't even need to worry about it.

      Sadly, I just blew away my development environment and don't have AJAX installed back in it yet so bear with me.

      If I recall correctly there is a textbox extender that can be used to allow you to manipulate your page in the OnChange event of your textbox. It's all done in the designer interface and as such you don't even need to worry about the code - that's all handled for you.

      I will respond again once I get AJAX installed back in my developer environment. If you figure it out in the meantime, let me know.

      Comment

      • progman417
        New Member
        • Mar 2008
        • 13

        #4
        Originally posted by balabaster
        I will respond again once I get AJAX installed back in my developer environment. If you figure it out in the meantime, let me know.
        Thanks for the idea, I had a quick look at the Ajax demos but I couldn't find anything that would do the trick (maybe I've not installed them all?)

        I tried putting the grid on a seperate update panel and having a button that hides the panel, then using __dopostback passing it the clientid of the button. It almost worked, but it threw a security warning about postbacks should only be triggered by server generated controls. Apparently you can get it to ignore it, but I don't want to be introducing potential security problems.

        Now I've got it half working - I've put the grid in a panel, and written a bit of javascript for the onFocus event that takes the clientid of the panel (div), and if it exists it sets it to display:none.

        It works, but if anything does a postback, it reappears!

        What I'm thinking of doing is to create a hidden text box, then in the javascript that hides the panel set the hidden text box to 1. Then in all the events that set the panel to visible, reset the text box, then in the page prerender event (or whichever is the last one before the page appears) if it is still set to 1, then reset it to 0 and hide the panel.

        I'll try it in the morning and let you know if it works!

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Okay...I played around a bit with the AJAX and it came up short for this, but what I've got is this:

          In my HTML page design, I have:

          <script type="text/javascript">
          <!--
          function SetDisplayPrope rty(elementId, displayProperty ){
          var obj = document.getEle mentById(elemen tId);
          if(obj.style.di splay != displayProperty ){
          obj.style.displ ay = displayProperty ;
          }
          }
          -->
          </script>

          <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="f alse"></asp:TextBox>
          <div style="display: block; border: solid 1px black;" id="ContentPane l">This is my content that disappears when I start typing...</div>

          Then in my codebehind I have
          TextBox1.Attrib utes.Add("onkey press", "SetDisplayProp erty('ContentPa nel', 'none');")

          This turns off my ContentPanel div when I start entering data into my textbox. The OnChanged event doesn't seem to fire it properly, so I attached it to OnKeyPress which seems to work just fine.

          Comment

          • progman417
            New Member
            • Mar 2008
            • 13

            #6
            Thanks balablaster,

            I decided to use the OnFocus event in the end - if someone has clicked into the field there's a fair chance they are going to change the value.

            The code I've used is:

            Code:
            txtValue.Attributes.Add("onFocus", "var pG = document.getElementById('panGrid'); if (pG) {pG.style.display='none'}; var tx = document.getElementById('txtHidePanel'); if (tx) {tx.value='1'}; ");
            txtHidePanel is set to 1 so that when the page is posted back it knows to hide the panel on the server side (otherwise it would reappear). In the show grid button I've set it to 0. In the pre_render event if it is still 1 then it hides the grid server side.

            Thanks for your help, I've been programming in ASP and PHP for over 8 years, I love the features in .net but it's quite daunting to get a handle on!

            Comment

            • balabaster
              Recognized Expert Contributor
              • Mar 2007
              • 798

              #7
              Stick with it - once you get your head around the basic concepts, the rest comes easily and fast. It's such a simple language once you've grasped the concepts - I love it.

              Comment

              Working...