ASP.NET如何将javascript函数添加到控件?

时间:2021-01-07 04:13:07

I have some user controls that I want to add some client side functionality to.

我有一些用户控件,我想添加一些客户端功能。

Let say 1 control has a hidden field and a bunch of checkboxes. When a checkbox is checked, it sets the hidden field to 'YES'. How could I $get that control in the hosting control or page, and call some function on it that would return the value of that hidden field?

假设1控件有一个隐藏字段和一堆复选框。选中复选框后,它会将隐藏字段设置为“是”。我怎么能在托管控件或页面中获取该控件,并在其上调用一些函数来返回该隐藏字段的值?

If I have a couple of these on the page, I'd like to be able to do this:

如果我在页面上有几个这样的话,我希望能够这样做:

var choices1 = $get('choices1_id')
if(choices1.dirty() = 'YES')
    //do whatever
var choices2 = $get('choices2_is')
if(choices2.dirty() = 'YES')
    //do whatever

I might be looking for something like this: http://jimblackler.net/blog/?p=23 but I'm not sure how to access the object(s) from the parent.

我可能正在寻找这样的东西:http://jimblackler.net/blog/?p = 23但我不确定如何从父级访问对象。

thanks, Mark

1 个解决方案

#1


I take it the problem you are running into is that ASP.Net can mangle the id of the control?

我认为你遇到的问题是ASP.Net可以破坏控件的id吗?

In that case What I normally do is either note how the id is mangled for simple pages, and write the javascript accordingly, or for more complex scenarios I'll check the .ClientID property of each control and put it into variables in a custom script that's easily accessible to the rest of the javascript on the page.

在那种情况下,我通常要么注意如何修改简单页面的id,并相应地编写javascript,或者对于更复杂的场景,我将检查每个控件的.ClientID属性并将其放入自定义脚本中的变量中这可以轻松访问页面上其余的javascript。

This should be easier than it is, and not having simple access to every control element from javascript is one of my complaints with the ASP.Net framework. They're doing a little to address this in the next version, though not as much as they could. In the mean time, you can find various components on the web that will help automate generating the script I described above.

这应该比它更容易,并且不能从javascript简单访问每个控制元素是我对ASP.Net框架的抱怨之一。他们在下一个版本中做了一些努力来解决这个问题,尽管没有那么多。与此同时,您可以在Web上找到有助于自动生成上述脚本的各种组件。

#1


I take it the problem you are running into is that ASP.Net can mangle the id of the control?

我认为你遇到的问题是ASP.Net可以破坏控件的id吗?

In that case What I normally do is either note how the id is mangled for simple pages, and write the javascript accordingly, or for more complex scenarios I'll check the .ClientID property of each control and put it into variables in a custom script that's easily accessible to the rest of the javascript on the page.

在那种情况下,我通常要么注意如何修改简单页面的id,并相应地编写javascript,或者对于更复杂的场景,我将检查每个控件的.ClientID属性并将其放入自定义脚本中的变量中这可以轻松访问页面上其余的javascript。

This should be easier than it is, and not having simple access to every control element from javascript is one of my complaints with the ASP.Net framework. They're doing a little to address this in the next version, though not as much as they could. In the mean time, you can find various components on the web that will help automate generating the script I described above.

这应该比它更容易,并且不能从javascript简单访问每个控制元素是我对ASP.Net框架的抱怨之一。他们在下一个版本中做了一些努力来解决这个问题,尽管没有那么多。与此同时,您可以在Web上找到有助于自动生成上述脚本的各种组件。