Search This Blog

Tuesday, February 1, 2011

Set focus to textbox after postback

In my web application which is developed using ajax, I had a problem. My textbox lost its focus after a postback, so I supposed to solve this issue with the textbox. Then I came with a solution using javascript. This may be helpful to some one

Put this javascript on your updatepanel page. You may have to change controls id strings.


<script type="text/javascript">
  var postbackElement;
  Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
  Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
 
  function beginRequest(sender, args) {
      postbackElement = args.get_postBackElement();
  }
        
  function pageLoaded(sender, args) 
    {
      var updatedPanels = args.get_panelsUpdated();
      if (typeof(postbackElement) === "undefined"
     {
          return;
      }
      else if (postbackElement.id.toLowerCase().indexOf('button1') > -1) 
       {
          for (i=0; i < updatedPanels.length; i++) { 
              alert('here');         
              document.getElementById('<%= TextBox1.ClientID %>').focus();
          }
      }
 
  }
</script>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.