Javascript to Get Post Back Element ID

Posted: August 24, 2010 in ASP.Net, Javascript

You can use the following piece of Javascript to get the post back element id:

Javascript (InitializeRequestHandler)
Copy this piece of code into your ASP.Net page. The first part of this carries out a InitializeRequestHandler to get the element id.

 
<script type="text/javascript">
   //-----------------------------------------------------
   // Script to get the element that caused the post back
   //-----------------------------------------------------
   var postBackElementID;
   Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(InitializeRequestHandler);
   function InitializeRequestHandler(sender, args){postBackElementID = args.get_postBackElement().id.substring(args.get_postBackElement().id.lastIndexOf("_") + 1);}
</script> 

Javascript (EndRequestHandler)
Copy this piece of code into your ASP.Net page. The second part of this carries out a EndRequestHandler to use the element id to do something.

 
<script type="text/javascript">
   //----------------------------------------------
   // Function to do something with the element id
   //----------------------------------------------
   function EndRequestHandler(sender, args)
   {
      if (postBackElementID == "your_element_id") 
      {   
         // Do something
      }
   }
</script> 
Advertisements
Comments
  1. Hershel says:

    OK, once your inserted this code, how do you find it at postback? (VB) Please?

    Thank you,
    Hershel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s