0

我正在创建一个asp页面。在该页面中,我定义了一个如下所示的属性

。CS

private long _sequence;
  public long Sequence { get { return _sequence; } set { _sequence = value; } }

现在我想在 js 文件中检索这个属性值。实际上我可以在 .aspx 中检索它,但我希望它在 .js 文件中。

这是我正在尝试的 js 函数和 .aspx 代码,但它找不到属性值

.aspx

<asp:Button ID="btnShowSimple" runat="server" Text="Notes Dialog" OnClientClick="NotesDialog(this)" />

.js

function NotesDialog(ctr) {
    var ControlName = document.getElementById(ctr.id);
    $("#btnShowSimple").click(function (e) {
        ShowDialog(false);
        e.preventDefault();
        LoadData("GetNotes", '"sequence":<%= this.Sequence %>');
    });
}

有什么我想念的吗??如果有人对此有任何想法,请帮助我..我两天后就面临这个问题..

4

3 回答 3

2

您的js文件是服务器上的静态文件。您不能<%= %>在其中使用这些标签。您可以通过在aspx页面中设置并在js文件中使用的全局 javascript 变量传递属性。

IE

.aspx

  <script type="text/javascript">
      myProp = <%= this.Sequence %>;
  </script>

.js

function NotesDialog(ctr) {
    var ControlName = document.getElementById(ctr.id);
    $("#btnShowSimple").click(function (e) {
        ShowDialog(false);
        e.preventDefault();
        LoadData("GetNotes", '"sequence":' + myProp);
    });
}
于 2012-06-27T06:53:01.757 回答
1

Nope you cannot do this in JS file as it is not processed by asp.net runtime. The best you can do is declare a variable in aspx and use it in js like:

aspx:

var _seq="<%= this.Sequence %>";

JS:

LoadData("GetNotes", '"sequence":' + _seq); //USE ASPX VARIABLE
于 2012-06-27T06:54:32.960 回答
0

You should try to separate you JavaScript code from you HTML.

Instead och creating a asp:Button use an HTML button and set an data-attribute you can retrive.

<button type="button" id="btnShowSimple" data-sequence="<%= this.Sequence %>">Notes Dialog</button>

And in your javascript-file bind an click event to your button that picks up the data-sequence.

/** Put this in the bottom of you javascript file **/
(function (window) {
    var document = window.document,
        view;

view = {
    /**
    * Invoked in jQuery event context
    * @param e
    */
    bindClickEvent : function (e) {
        $("#btnShowSimple").click(function (e) {
            e.preventDefault();
            var sequence = $(this).data('sequence');

            ShowDialog(false);

            LoadData("GetNotes", '"sequence":' + sequence);

        });
    }
}

$(document).ready(view.bindClickEvent);

} (window));

From your current code you when you click the asp:Button you just bind a new click event and never execute it.

Also from some of the other answer, you should NEVER declare arbitrary global variables in JavaScript

于 2012-06-27T07:01:05.437 回答