admin管理员组

文章数量:1323723

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

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

Share Improve this question edited Feb 16, 2011 at 11:08 Shadow Wizzard 66.4k26 gold badges146 silver badges209 bronze badges asked Feb 15, 2011 at 21:07 RyanRyan 5,50625 gold badges77 silver badges129 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

本文标签: aspnettrigger automatic postback in javascriptStack Overflow