JQuery / ASP.NET 新手关于的问题
大家好,在获取 asp 按钮与 JQuery 交互时遇到问题。我基本上试图隐藏一个包含表单的 div 并将其替换为处理图像。当我使用 HTML 输入按钮作为触发器时,它对我来说工作正常,但当我使用 aspButton 时,什么也没有发生。
这可行(HTML 按钮的 id 是“btnSubmit”):
<script>
$('#btnSubmit').click(function () {
$('#form1').fadeOut('fast', function () {
$('#processing').fadeIn('fast', function () {
});
});
});
</script>
这不行(ASP 按钮的 id 是“btnSubmitASP”):
<script>
$('#btnSubmitASP').click(function () {
$('#form1').fadeOut('fast', function () {
$('#processing').fadeIn('fast', function () {
});
});
});
</script>
知道让 asp 按钮执行此操作的技巧是什么吗?
谢谢
Hey all, having an issue getting asp buttons to interact with JQuery. I'm basically trying to hide a div that contains a form and replace it with an processing image. It works fine for me when I use an HTML input button as the trigger but when I use an aspButton nothing happens.
This works (the id of the HTML button is 'btnSubmit'):
<script>
$('#btnSubmit').click(function () {
$('#form1').fadeOut('fast', function () {
$('#processing').fadeIn('fast', function () {
});
});
});
</script>
This doesn't (the id of the ASP button is 'btnSubmitASP'):
<script>
$('#btnSubmitASP').click(function () {
$('#form1').fadeOut('fast', function () {
$('#processing').fadeIn('fast', function () {
});
});
});
</script>
Any idea what the trick is to get the asp button to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
该控件的 ASP.net 服务器 ID 与 html ID 不同。 (ASP.net 将此称为客户端 ID)。您可以通过以下方式获取客户端 ID:
The ASP.net server ID for the control is different from the html ID. (ASP.net calls this the client ID). You can get the client id this way:
如果您使用的是 asp.net 4.0,您可以设置按钮的 ClientIDMode 属性='Static'。这将阻止运行时破坏 ID。
If you are using asp.net 4.0 you can set the button's ClientIDMode property ='Static'. This will stop the runtime from mucking with the ID.
试试这个:
说明:
当所有控件发送到客户端时,ASP.NET 会重命名它们。因此,您的 ASP.NET 按钮没有客户端 ID“btnSubmitASP”客户端。上面的代码调用服务器端的服务器控件并获取其客户端 ID 以在 jQuery 代码中使用。
或者,您可以使用 jQuery 选择器:
这将查找客户端 ID 以“_btnSubmitASP”结尾的控件。
Try this:
Explanation:
ASP.NET renames all of its controls when they get sent to the client. Consequently, your ASP.NET Button does not have a client ID of "btnSubmitASP" client-side. The above code calls the server control on the server side and gets its client-id to use in the jQuery code.
Alternatively, you can use jQuery selectors:
This will look for controls whose client ID ends with "_btnSubmitASP".
使用 ClientId 的另一种替代方法是为 ASP:button 分配一个唯一的类。您的选择器将如下所示:
Another alternative to using the ClientId is to assign a unique class to the ASP:button. Your selector would then look like this:
对于 ASP.NET 按钮,您应该使用
OnClientClick
属性,因为它已将内置客户端脚本添加到按钮以执行其回发行为。示例:如果您在
OnClientClick
中返回false
,您将阻止按钮的默认行为阻止PostBack
。不执行任何操作或返回 true 将导致发生PostBack
。通过使用此方法,您无需知道Button
的名称即可附加脚本代码。为了让您的代码正常工作,您需要内联控件的
ClientID
来创建脚本,因此更改以下行以使用ClientID
属性>Button:您需要获取
ClientID
,因为 ASP.NET 将名称添加到名称空间中并防止名称重复。如果您查看 ASP.NETButton
,您会注意到name
和ID
属性添加了很多内容,例如:For ASP.NET buttons you should use the
OnClientClick
property as it has built in client side scripting added to the button to do its post back behavior. Example:If you return
false
in theOnClientClick
you will prevent the default behavior of the button preventing aPostBack
. Doing nothing or returning true will cause thePostBack
to occur. By using this method you don't need to know the name of yourButton
to attach the script code.To just get your code working though, you need to get the
ClientID
of the control inline to creating you script so change the following line to use theClientID
property of theButton
:You need to get the
ClientID
because ASP.NET adds to name to namespace it and prevent duplication of names. If you look at the ASP.NETButton
, the you will notice thename
andID
properties have a lot more added to it like:我对 jQuery 的了解很浅,但我可以给你一个提示:记住 jQuery 是在客户端执行的,而 ASP 按钮是在服务器上呈现并在响应中返回的。
当您的页面从服务器返回时,请仔细检查按钮的 HTML 标记,并确保其结构符合您的预期。例如,可能未按预期设置 ID 属性。
My knowledge of jQuery is very shallow, but I can give you one tip: Remember that jQuery is being executed client-side, while the ASP button is rendered on the server and returned in the response.
Double-check the HTML markup for the button when your page is returned from the server, and make sure it's structured as you expect. Perhaps the ID attribute isn't being set as expected, for example.
您的按钮控制器是 runat="server",因此这意味着 .NET 将在将控制器渲染为 HTML 之前修改控制器的 id。
jQuery 尝试使用该 ID 来执行您想要执行的任何操作。但ID已经不一样了。
在按钮上使用类。我知道它不如 ID 快,但这是最好的方法,因为 .NET 不会修改您的 css 类。
Your button controller is runat="server" so this means that .NET will modify the controller's id before rendering it in HTML.
jQuery tries to use that ID to do whatever you want to do with it. But the ID is no longer the same.
Use a class instead on your button. I know it's not as fast as an ID, but it's the best way to do it because .NET will not modify your css class.
如果您的 ASP:Button 包含
runat="server"
,那么 .NET 将在到达 DOM 之前修改 ID 值,因此您生成的可能会出现错误因此
,您的 jQuery 选择器
$('#btnSubmitASP')
不再有效,因为 ID 已更改。使用 Firebug 或右键单击 ->查看源代码以确认实际的 ID 值。
If your ASP:Button contains
runat="server"
then .NET will modify the ID value before it get to the DOM, so your resulting<input>
will probably wind up looking likeTherefore, your jQuery selector
$('#btnSubmitASP')
is no longer valid because the ID has changed.Use Firebug or Right click -> View source to confirm the actual ID value.