4

I'm trying to execute some JavaScript on page load on a custom page on a SharePoint site (it populates the people picker with the current user). The problem is that the code executes on postback too, which I don't want as it will reset any changes to the people picker.

I've tried using if(!IsPostBack) to no avail. Everything errors out at that point, giving

SCRIPT5009: 'IsPostBack' is undefined.

I can't find anything online to help with this. Any ideas? Thanks

4

3 回答 3

6

你可以像这样创建一个函数:

function IsPostBack() {
    var ret = '<%= Page.IsPostBack%>' == 'True';
    return ret;
}
于 2016-03-03T17:56:09.410 回答
3

IsPostBack不是 javascript 变量,它是仅在服务器上可用的 .NET webforms 变量,因此客户端会抱怨它。那么该怎么办呢?我建议在您的控件的 html 中使用这种混搭:

<% if(IsPostBack) { %> <!-- runs on server -->

<script type="text/javascript">
 alert('will only be printed to html if not postback');
</script>

<% } %> <!-- ends server if-block -->
于 2014-11-17T23:19:26.333 回答
1

您可能想尝试以下方法。使用 JavaScriptpageLoad方法,使用对象的isInAsyncPostBackPropertyPageRequestManager来判断是否是回发。有关详细信息,请参阅此处的 MSDN 链接。

<script type="text/javascript" language="javascript">
  function pageLoad(sender, args) {
    if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {

     // call you JavaScript function in here

    }
  }
</script>
于 2014-11-17T17:36:49.117 回答