Moving the cursor back to the textbox which did the post back

Last post 04-19-2008 6:00 AM by chetan.sarode. 4 replies.

Sort Posts:

  • Moving the cursor back to the textbox which did the post back

    04-18-2008, 9:39 AM
    • Loading...
    • hulkwsu
    • Joined on 03-17-2008, 3:09 AM
    • Posts 21

    Hello All,

     I have 5 rows of 6 column textboxes in my gridview, on the text change event of the textbox i am doing some calculations with the values in the textbox.Everything works fine.The problem is,after the calculation the cursor  gets lost.I want the cursor to be either in the same textbox, or to the next textbox if tab is pressed or in any other textbox where the user clicks after changing the values.

    can any one help me in this.
     

  • Re: Moving the cursor back to the textbox which did the post back

    04-18-2008, 10:13 AM
    • Loading...
    • whatispunk
    • Joined on 03-06-2007, 5:21 PM
    • Winnipeg
    • Posts 497

    Are you doing a postback to perform these calculations?

    Do you need to do these calculations on the server? If not, then javascript would be a much better choice here. But if you must do the calculations on the server then you should use a PageMethod.

    A PageMethod is like calling a webservice from javascript, but the method resides in your code-behind, you can call it from javascript, and no postback occurs.

    To create a PageMethod there are a few things you must do.

    1. Set EnablePageMethods="True" in your ScriptManager

    2. Create a public static method in your code behind with the attribute [System.Web.Services.WebMethod() ]

    3. The return value of this method can be of any type, but in your case should use a string to return to the textbox.

    4. In your javascript create 3 functions:

    function CallYourPageMethod(c) { // c is optional, but it will allow you to pass a reference to your control into the function
      PageMethods.NameOfPublicStaticMethod(parameter1, parameters2, ... parameterN, onSuccessCallBack, onFailureCallBack, userContext);
    }
    
    function onSuccessCallBack(result, userContext) {
      if (result) {
        $get(userContext.id).innerText = result;
      }
      else {
        alert("An unexpected error occurred");
      }
    }
    
    function onFailureCallBack(error, userContext) {
      if (error) {
        alert("The control " + userContext.id + " caused the following error:\n" + error.get_message());
      }
      else {
        alert("An unexpected error occurred");
      }
    }
    
    

    A bit more detail about the code above will help too. (No idea why this turned blue).


    The following call: PageMethods.NameOfPublicStaticMethod(parameter1, parameters2, ... parameterN, onSuccessCallBack, onFailureCallBack, userContext);
    takes several parameters. The first N parameters must match the parameters of your code-behind method. In your case you may only require to the text of the textbox, so you'll just have one parameter here. Then following those parameters are 2 callback methods. These will handle the response from the server (success and failure) then you can pass in an optional userContext (in your case I'd pass in a reference to your control, or at the least it's ID)

    In the following functions you specify what your callbacks will do. Each callback takes 2 parameters (3 actually, but you don't need the 3rd one).

    Anyways, I'm sure you'll have questions, but play around with this. PageMethods are pretty frickin amazing.

    Read more: http://asp.net/AJAX/Documentation/Live/tutorials/ExposingWebServicesToAJAXTutorial.aspx  -- Check out the bottom of the page "Calling Static Methods In An ASP.NET Web Page"

    Calling PageMethods is just like calling WebServices from javascript. Only you don't have to configure a webservice.

    Good luck!

    Don't forget to click "Mark as Answer" if someone answers your question. Feel free to mark more than one answer, if you feel so inclined.
  • Re: Moving the cursor back to the textbox which did the post back

    04-18-2008, 3:50 PM
    • Loading...
    • hulkwsu
    • Joined on 03-17-2008, 3:09 AM
    • Posts 21

    I wonder how this helps my problem 

  • Re: Moving the cursor back to the textbox which did the post back

    04-18-2008, 4:47 PM
    Answer
    • Loading...
    • whatispunk
    • Joined on 03-06-2007, 5:21 PM
    • Winnipeg
    • Posts 497

    By the sound of your problem you were triggering a postback when your textbox changed. So when the postback finished the textbox no longer had focus. This way, the postback will never occur, and the focus will remain where you want it.

    Not to mention, the page will be far more responsive, and your server will thank you for not having to postback your entire page (and all that gridview data) ever time a user goes from one textbox to another.

    It helps your problem in more than one way.

    Don't forget to click "Mark as Answer" if someone answers your question. Feel free to mark more than one answer, if you feel so inclined.
  • Re: Moving the cursor back to the textbox which did the post back

    04-19-2008, 6:00 AM
    Answer

    http://forums.asp.net/p/1249475/2308333.aspx 

    Sets the browser focus to the specified control

    http://www.asp.net/AJAX/Documentation/Live/mref/O_T_System_Web_UI_ScriptManager_SetFocus.aspx

     

    You can use the following code to set the focus:
     

    ScriptManager sm = ScriptManager.GetCurrent(this);
    sm.SetFocus(myTextBox);

    To set focus and select text of a you have to use the following:
     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);
    http://forums.asp.net/t/1099345.aspx
    http://forums.asp.net/t/1199709.aspx
    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
Page 1 of 1 (5 items)