2

I want to know Id update panel that initial a request in JavaScript .I write this script but it return undefined.

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender, args) {
    alert(sender.ID);
}
function EndRequest(sender, args) {
}

sender is not null and it return [object] but How I can get ID?


Edit 1)

I think when UpdatePanel be inside MasterPage it does not work.this is my code:

<script type="text/javascript">
    $(document).ready(function () {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(InitializeRequest);
        prm.add_endRequest(EndRequest);


        function InitializeRequest(sender, args) {
            var UpdPanelsIds = args.get_updatePanelsToUpdate();
            alert(UpdPanelsIds[0]);
        }
        function EndRequest(sender, args) {
            if ($('.AlarmLogo').val() == "3") {
                alert('nima');
            }
        }
    });


</script>

and :

<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="timer" Interval="4000" runat="server" OnTick="timer_Tick" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="pnlAlarm" runat="server" CssClass="pnlAlarm" ClientIDMode="Static">
            <a href="#">
                <div id="Alarm">
                    <asp:TextBox ID="lblContent" runat="server" Text="HHHEEELLLOOO" CssClass="AlarmLogo" ClientIDMode="Static"></asp:TextBox>
                </div>
            </a>
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer" />
    </Triggers>
</asp:UpdatePanel>
    <div class="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </div>
</form>

and code behind:

protected void Page_Load(object sender, EventArgs e) 
{     
    if (!IsPostBack)     
    {         
        Session["nima"] = 1;    
    } 
} 

protected void timer_Tick(object sender, EventArgs e) 
{    
} 
wonea
  • 4,783
  • 17
  • 86
  • 139
Arian
  • 12,793
  • 66
  • 176
  • 300

1 Answers1

5

You can use the get_updatePanelsToUpdate that return an array with the Ids of the UpdatePanels that will be going to updated.

<script>
    window.onload = function() {
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        prm.add_initializeRequest(InitializeRequest);
        prm.add_endRequest(EndRequest);
    };

      function InitializeRequest(sender, args) 
      {     
         // get the array of update panels id
         var UpdPanelsIds = args.get_updatePanelsToUpdate();
         // get the Post ID
         args.get_postBackElement().id;
      }

      function EndRequest(sender, args) {
      }
</script>

http://msdn.microsoft.com/en-us/library/ee224805.aspx

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • thanks but I used `alert(UpdPanelsIds);` and i got an empty message box and `alert(UpdPanelsIds[0]);` and I got `undefiend` message box – Arian Apr 06 '12 at 09:17
  • try this `console.log(args.get_updatePanelsToUpdate());` on google chrome to see the array (of course open the console) – Aristos Apr 06 '12 at 09:18
  • @Kerezo I just test it and work to me fine - check if you have any javascript error - or if you do not include the script manager before that call ! The script manager take care to include this functions, if this script not found them then is not work. This is script from MS – Aristos Apr 06 '12 at 09:20
  • @Kerezo and the alert work on me - you probably have javascript errors. – Aristos Apr 06 '12 at 09:23
  • @Kerezo and my sender is the same way, I use also Master page... check maybe this return the id you looking for `args.get_postBackElement().id` (but is not the update panel but the post id) – Aristos Apr 06 '12 at 09:35
  • `args.get_postBackElement().id` returns `timer1`.please copy all of code iside a Master Page. can you please include your codes in your answer? thanks – Arian Apr 06 '12 at 09:41
  • I got the problem If you use `Trigger` , `args.get_updatePanelsToUpdate` return nothing – Arian Apr 06 '12 at 19:26