简体   繁体   中英

trigger automatic postback in javascript

I do not have access to the <body> tag because it is in masterpage. I want to trigger an automatic postback when the page load as:

<script type="text/javascript">
       window.onscroll=  __doPostBack("<%= button.ClientID %>", "");
</script>

where should I put this code? I get a Not implemented JS error if I place it just before the </asp:Content> tag.

Any idea how I should do this ?

PS: I need to trigger that postback because I want to populate an updatepanel when the page loads

You can have such code and place it anywhere:

<% if (!Page.IsPostBack) { %>
<script type="text/javascript">
window.onload = function() {
   __doPostBack("<%= button.ClientID %>", "");
}
</script>
<% } %>

Assuming you're using C# - if you have VB.NET the syntax will be bit different.

Edit: to avoid using <% and %> you can have this in the Page_Load of your page:

if (!Page.IsPostBack) {
   this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "auto_postback", "window.onload = function() { __doPostBack(\"" +  button.ClientID + "\", \"\"); }; ", true);
}

Edit II: alternative way with better chance of working is to have such JS code instead:

"window.onload = function() { var buttonID = '" +  button.ClientID + "'; alert('ID: ' + buttonID + ', clicking...'); document.getElementById(buttonID).click(); }; "

This will hopefully show you the client ID of the button then auto click it. If no luck make sure the ID is correct and really exist in the document and we'll try to find what is the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM