How can I get the actual ID from get_postBackElement

Last post 05-07-2009 10:42 AM by atconway. 4 replies.

Sort Posts:

  • How can I get the actual ID from get_postBackElement

    05-06-2009, 12:17 PM
    • Contributor
      5,664 point Contributor
    • atconway
    • Member since 09-24-2007, 9:20 PM
    • Florida U.S.A
    • Posts 1,186

    I have the following JS code extracted from the following link (http://disturbedbuddha.wordpress.com/2007/12/12/handling-multiple-asynchronous-postbacks/#comment-184)

       var prm = Sys.WebForms.PageRequestManager.getInstance();
       prm.add_initializeRequest(InitializeRequestHandler);
       prm.add_endRequest(EndRequestHandler);
    
       var pbQueue = new Array();
       var argsQueue = new Array();
    
       function InitializeRequestHandler(sender, args) {
          if (prm.get_isInAsyncPostBack()) {
             args.set_cancel(true);
             pbQueue.push(args.get_postBackElement().id);
             argsQueue.push(document.forms[0].__EVENTARGUMENT.value);
          }
       }
    
       function EndRequestHandler(sender, args) {
          if (pbQueue.length > 0) {
             var postbackID = pbQueue.shift().valueOf();
             __doPostBack(postbackID, '');
          }
       }

     The problem I am having is with the ID's of the controls. The ID (actually the UniqueID) that originally caused the postback is as follows:  ctl100$ContentPlaceHolderMyApp$btnHidden   (This is the good ID that I need)

    This is the exact UniqueID that must be used when calling __doPostBack.  However, once I extract the ID from the wired up 'InitializeRequestHandler' function via args.get_postBackElement().id I get the following slighly different control ID: ctl100_ContentPlaceHolderMyApp_btnHidden

    The problem is I must extract that 1st id with the ($) dollar signs as it represents the UniquieID of the control causing the postback.  I need it in order to call __doPostBack and successfully have the postback occur.

    Does anyone know how I can get the proper UniqueID I am looking for from 'get_postBackElement' or any other way (preferably not a find-and-replace _ to $ method)??  Confused

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: How can I get the actual ID from get_postBackElement

    05-06-2009, 2:18 PM
    • Participant
      1,445 point Participant
    • avidyarthi
    • Member since 09-20-2006, 2:00 PM
    • California
    • Posts 295

    Have you tried:

    var quantity = document.getElementById('<%=tbx_quantity.ClientID%>').value;

     

    Thanks,

    avidyarthi.
  • Re: How can I get the actual ID from get_postBackElement

    05-06-2009, 2:50 PM
    • Contributor
      5,664 point Contributor
    • atconway
    • Member since 09-24-2007, 9:20 PM
    • Florida U.S.A
    • Posts 1,186

    No see the code I provided sort of 'intercepts' calls that have already been made to '__doPostBack' and places them in a queue to be ran 1 by 1 so none of them get dropped due to the nature of how asyncronus post back calls are handled.  Therefore, the actual ID has alrady been passed to the main call to '__doPostBack', and I just need to extract it back out.  I have several separate functions that call __doPostBack on thier own, so I can not hardcode in the code I posted the control name because I don't know which one it is that was making the call.

    The problem is 'args.get_postBackElement().id' gets the ID from the originating call, but it is equivilent to the ID of the control and not the UniquieID that was used to make the originating __doPostBack call.  I was hoping that 'args.get_postBackElement().name' or 'args.get_postBackElement().uniquieID' exsiting or something but they do not.

    Right now I am experimenting with sending the UniquieID in both the 1st and 2nd parameter of the originating doPostBack call, and then extracting out the 2nd parameter when its turn is up in the queue to actually process the _doPostBackCall with the follwing

    document.forms[0].__EVENTARGUMENT.value

    I still want to know if there is a way to extract out the orginal ID that I sent to the 1st doPostBack call which represents the UniquieID of the control, so does anybody know?

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: How can I get the actual ID from get_postBackElement

    05-07-2009, 1:53 AM
    Answer
    • Star
      8,473 point Star
    • Pawan_Mishra
    • Member since 03-13-2008, 7:37 AM
    • Bangalore
    • Posts 1,249

    Hi

    the id which you are trying to get is actually the UniqueId property of elements . And as such I didnt find any article wherein it is explained on how to access UniqueId property in javascript . On the other hand the id which you are getting is the ClientId property of elements which you can acess directly by using the following syntax :- $get('<% = Button1.ClientID %>');

    Now in order to access the UniqueID property or say "name" attribute of rendered alements ( if you view the source of the page you will notice that the id which you are looking for is rendered as name attribute of elements) , I used jQuery to access the name attribute . Here is the sample code :-

    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(begin);
    function begin(sender, args) {
        alert(args.get_postBackElement().id); -->ctl00_cntChildPage_Button1

        var elem = $("#" + args.get_postBackElement().id).attr("name");
        alert(elem); -->ctl00$cntChildPage$Button1
    }

    If you are not using jQuery in your application then integration of it is just a peice of cake . Download the javascript file and add it in your page using <script> tags . Thats it.

    Regards
    Pawan Mishra

    Too many eyes doesn't make a good code !!!

    .Net 360°
  • Re: How can I get the actual ID from get_postBackElement

    05-07-2009, 10:42 AM
    Answer
    • Contributor
      5,664 point Contributor
    • atconway
    • Member since 09-24-2007, 9:20 PM
    • Florida U.S.A
    • Posts 1,186

    Pawan -

    I don't have jQuery integrated, but it looks like based on the response from the 'alert' that the code you provided will return the ID I need, so I marked yours as an awnser. 

    I came up with another soltuion as well.  I ended up passing the exact controlID (UniqueID), the one that initiated the doPostBack, as the 2nd parameted provided to the originating doPostBack call since I was previously passing it nothing ('').  Then that parameter/eventarg was extracted as follows and shown above:

    document.forms[0].__EVENTARGUMENT.value

    So basically the originating doPostBack call is passed the same ID for both the 1st and 2nd parameter.  While this works well for me, because I can get the 2nd parameter containing the raw UniqueID param passed in, it may not work for others who are already using the 2nd parameter of the doPostBack call to pass some other value.  The solution would be to pass an array of values to that 2nd parameter and know which one was the original control ID to extract out, or to use the jQuery solution from above.

    One important note about this thread too, the reason I need to use 'UniqueID' when calling the doPostBack call is because I have a master page and a content page.  Using the ConrtolID of a control to trigger a doPostBack on a content page does not work, and the UniqueID must be used.  Had this been a regular .aspx page, the original code I used would have worked perfectly with no modifications or preservation of the originating control ID needed.

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

Page 1 of 1 (5 items)