如何从ServerControl访问ClientID的位于外部JavaScript [英] How to access the ClientID from a ServerControl in an external JavaScript

查看:171
本文介绍了如何从ServerControl访问ClientID的位于外部JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,如果我在我的SharePoint项目中使用JavaScript,我添加了code到* .ascx文件,在<脚本类型=文/ JavaScript的> < / SCRIPT> 块,并为每个元素的变量客户端ID

At the moment, if I use JavaScript in my SharePoint projects, I add the code into the *.ascx file, in a <script type="text/javascript"></script> block and create for each element a variable for the ClientID.

例如:

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

现在我想将一个外部JavaScript添加到我的项目,并插入code那里。
但是,我怎么能访问到客户端ID ?在外部JavaScript我不能使用&LT;%= TextBox1.ClientID%GT; 。我发现这一点:<一href=\"http://stackoverflow.com/questions/911042/ajax-asp-net-referencing-server-controls-in-external-file\">referencing在外部文件服务器控件,但我不明白,这应该是如何工作的。这将是真棒,如果有人可以解释我的,如何访问的标识。

Now I would like to add an external JavaScript to my projects and insert the code there. But how could I access to the ClientID? In the external JavaScript I can’t use <%= TextBox1.ClientID %>. I found this: referencing server controls in external file but I doesn’t understand, how this should work. It would be awesome, if someone could explain my, how to access the ids.

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

By the way, why this:

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

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

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

doesn’t work, no message would be shown?

格尔茨

编辑1:

好吧,我可以只使用textBox1的在我的外部脚本?
我就是这么做的,这是我在* .ascx文件:

Okay, I could just use textBox1 in my external script? I did it this way, this is in my *.ascx file:

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

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

In my external script I have just a function to test it:

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

我也与测试了它#+ 。每次测试()被执行,我收到以下错误:

I also tested it with "#" +. Every time test() is executed, I get following error:

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

编辑2:
我错过了一个中的警报。但现在我得到这个变量没有被定义的消息。
如果我使用: $('#'+ ClientIDs.SumbitSearch).VAL()我刚刚得到的文本,而不是我控制的ID

Edit 2: I missed a ) in the alert. But now I get a message that the variable is not defined. If I use: $('#' + ClientIDs.SumbitSearch).val() I just get the Text and not the ID of my control.

修改3:
目前我使用的:

Edit 3: At the moment I use:

<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中这将是很好,如果他让我知道。

In my *.ascx file, it works. I don't like that way... It doesn't work in a external JS, the references doesn't work. If someone have some other ideas, which would work with .net 3.5 it would be nice, if he let me know.

推荐答案

要解释,并简化了你链接到这个问题,他们所做的一切是设置从页面/服务器控件的JavaScript变量,以及读取从外部JavaScript文件变量。

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.

例如,您的* .ascx文件将包含这段JavaScript:

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

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

随后,外部JavaScript文件只需引用变量 textBox1的

现在,还有其他的方式来做到这一点。如果您使用ASP.NET 4,您可以使用新属性的的ClientIDMode 从改变你的ID prevent ASP.NET。如果你不使用ASP.NET 4中,也只是一个CSS类添加到您想要选择的元素,只是改变你的jQuery选择使用一个类(不是使用一个ID,虽然速度稍慢)。

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).

最后,您需要使用时,评估为元素ID jQuery选择,所以这将工作

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

alert($('#' + ClientIDs.test1).val());

这篇关于如何从ServerControl访问ClientID的位于外部JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆