JQuery / ASP.NET 新手关于的问题

发布于 2024-09-15 19:06:53 字数 734 浏览 5 评论 0原文

大家好,在获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

岁月染过的梦 2024-09-22 19:06:54

该控件的 ASP.net 服务器 ID 与 html ID 不同。 (ASP.net 将此称为客户端 ID)。您可以通过以下方式获取客户端 ID:

$('#<%= this.btnSubmitASP.ClientID %>').click( /* etc */ );

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:

$('#<%= this.btnSubmitASP.ClientID %>').click( /* etc */ );
你是我的挚爱i 2024-09-22 19:06:54

如果您使用的是 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.

开始看清了 2024-09-22 19:06:54

试试这个:

<script>
    $('<%=btnSubmitASP.ClientID%>').click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

说明:
当所有控件发送到客户端时,ASP.NET 会重命名它们。因此,您的 ASP.NET 按钮没有客户端 ID“btnSubmitASP”客户端。上面的代码调用服务器端的服务器控件并获取其客户端 ID 以在 jQuery 代码中使用。

或者,您可以使用 jQuery 选择器:

<script>
    $("[id$='_btnSubmitASP']").click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

这将查找客户端 ID 以“_btnSubmitASP”结尾的控件。

Try this:

<script>
    $('<%=btnSubmitASP.ClientID%>').click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

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:

<script>
    $("[id$='_btnSubmitASP']").click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

This will look for controls whose client ID ends with "_btnSubmitASP".

二货你真萌 2024-09-22 19:06:54

使用 ClientId 的另一种替代方法是为 ASP:button 分配一个唯一的类。您的选择器将如下所示:

<asp:button runat="server" CssClass="submitbutton">/<asp:button>

<script>
        $("submitbutton").click(function () {
            $('#form1').fadeOut('fast', function () {
                $('#processing').fadeIn('fast', function () {
                });
            });
        });
 </script>

Another alternative to using the ClientId is to assign a unique class to the ASP:button. Your selector would then look like this:

<asp:button runat="server" CssClass="submitbutton">/<asp:button>

<script>
        $("submitbutton").click(function () {
            $('#form1').fadeOut('fast', function () {
                $('#processing').fadeIn('fast', function () {
                });
            });
        });
 </script>
逆夏时光 2024-09-22 19:06:54

对于 ASP.NET 按钮,您应该使用 OnClientClick 属性,因为它已将内置客户端脚本添加到按钮以执行其回发行为。示例:

<asp:Button ID="btnSubmitASP" runat="server"
    OnClientClick="yourJqueryFunction();" />

如果您在 OnClientClick 中返回 false,您将阻止按钮的默认行为阻止 PostBack。不执行任何操作或返回 true 将导致发生PostBack。通过使用此方法,您无需知道 Button 的名称即可附加脚本代码。

为了让您的代码正常工作,您需要内联控件的 ClientID 来创建脚本,因此更改以下行以使用 ClientID 属性>Button:

$('#<%= btnSubmitASP.ClientID %>').click(function () {   

您需要获取ClientID,因为 ASP.NET 将名称添加到名称空间中并防止名称重复。如果您查看 ASP.NET Button,您会注意到 nameID 属性添加了很多内容,例如:

<input type="submit" name="ctl00$ContentPlaceHolder1$btnSubmitASP" value="Test"
    id="ctl00_ContentPlaceHolder1_btnSubmitASP" />

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:

<asp:Button ID="btnSubmitASP" runat="server"
    OnClientClick="yourJqueryFunction();" />

If you return false in the OnClientClick you will prevent the default behavior of the button preventing a PostBack. Doing nothing or returning true will cause the PostBack to occur. By using this method you don't need to know the name of your Button 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 the ClientID property of the Button:

$('#<%= btnSubmitASP.ClientID %>').click(function () {   

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.NET Button, the you will notice the name and ID properties have a lot more added to it like:

<input type="submit" name="ctl00$ContentPlaceHolder1$btnSubmitASP" value="Test"
    id="ctl00_ContentPlaceHolder1_btnSubmitASP" />
人疚 2024-09-22 19:06:54

我对 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.

听风念你 2024-09-22 19:06:54

您的按钮控制器是 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.

我最亲爱的 2024-09-22 19:06:54

如果您的 ASP:Button 包含 runat="server",那么 .NET 将在到达 DOM 之前修改 ID 值,因此您生成的 可能会出现错误因此

<input id="ctl00_ContentPlaceHolder1_btnSubmitASP" />

,您的 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 like

<input id="ctl00_ContentPlaceHolder1_btnSubmitASP" />

Therefore, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文