1

目前,如果我在我的 SharePoint 项目中使用 JavaScript,我会将代码添加到 *.ascx 文件中的一个<script type="text/javascript"></script>块中,并为每个元素创建一个用于ClientID.

例如:

var test = '<%= TextBox1.ClientID %>';

现在我想在我的项目中添加一个外部 JavaScript 并在那里插入代码。但是我怎么能访问ClientID?在我不能使用的外部 JavaScript 中<%= TextBox1.ClientID %>。我发现了这个:在外部文件中引用服务器控件,但我不明白,这应该如何工作。如果有人可以解释我的如何访问这些 ID,那就太棒了。

顺便说一句,为什么这样:

<script type="text/javascript">
    var ClientIDs = {
        test1   : '<%= TextBox1.ClientID %>',
        test2   : '<%= TextBox2.ClientID %>'
        }

    function SetButtonStatus() {    
            alert($(ClientIDs.test1).value);
        }
</script>

不起作用,不会显示任何消息?

格雷茨

编辑1:

好的,我可以在我的外部脚本中使用 textBox1 吗?我是这样做的,这是在我的 *.ascx 文件中:

<script type="text/javascript">
    var ClientIDs = {
        textBox1:    '<%= textBox1.ClientID %>',
        textBox2:    '<%= textBox2.ClientID %>'
    }
</script>

在我的外部脚本中,我只有一个函数来测试它:

function test () {
alert($(ClientIDs.textBox1).val();
}

我也用"#" +. 每次执行 test() 时,我都会收到以下错误:

"document.getElementById(...)" is null or not an object

编辑 2: 我错过了)警报中的一个。但现在我收到一条消息,指出该变量未定义。如果我使用:$('#' + ClientIDs.SumbitSearch).val()我只得到文本而不是控件的 ID。

编辑3: 目前我使用:

<script type="text/javascript">
    var ClientIDs = {
        test1 :    '<%= TextBox1.ClientID %>',
        test2 :    '<%= TextBox2.ClientID %>'
    }

    function test() {
       alert($('#' + ClientIDs.test1).attr("id")));
    }
</script>

在我的 *.ascx 文件中,它可以工作。我不喜欢这种方式......它在外部 JS 中不起作用,引用不起作用。如果有人有其他想法,可以与 .net 3.5 一起使用,如果他让我知道,那就太好了。

4

4 回答 4

3

To explain, and simplify the question that you're linking to, all they are doing is setting a JavaScript variable from the page/server control, and reading that variable from an external JavaScript file.

For example, your *.ascx file will contain this JavaScript:

var textBox1 = '<%= TextBox1.ClientID %>';

Then, your external JavaScript file can just reference the variable textBox1.

Now, there are other ways to accomplish this. If you're using ASP.NET 4, you can use a new property ClientIDMode to prevent ASP.NET from changing your IDs. If you're not using ASP.NET 4, could also simply add a CSS class to the elements you want to select, and just change your jQuery selector to use a class (slightly slower than using an ID though).

Lastly, you'll need to use the # when evaluating a jQuery selector for an element id, so this will work:

alert($('#' + ClientIDs.test1).val());
于 2011-10-24T14:04:11.543 回答
1

In the ASP page:

<script>
    var test2 = '<%= TextBox2.ClientID %>'
</script> 

And in external JavaScript access TextBox2 in this way:

documnet.getelementByid(test2);
于 2012-08-27T06:46:06.240 回答
0

I've never found a solution to this that I like, but I've implemented a couple work-arounds that aren't completely horrible:

  • For the JS code which must reference specific page elements, define it in the .aspx file, so you have access to <%= btnFoo.ClientID %>. Those functions can call common heavy-lifting functions in your .js file.
  • Define javascript variables in your .aspx file to hold the ClientID values. Functions in your .js file can reference those variables to get the client IDs. Of course, those functions will only work correctly on pages which define the expected variables.

Perhaps other SO'ers will have more elegant solutions to this problem - I look forward to seeing other responses.

于 2011-10-24T14:05:02.823 回答
0

Use explicity global parameter.

window.clientId= '<%= TextBox1.ClientID %>';

it means that you have gloabal variable "cilentId". And then you can use it everywhere.

于 2011-10-25T12:06:44.843 回答