简体   繁体   English

jQuery和ASP.NET页面加载

[英]jQuery and ASP.NET Page load

I test jQuery and ASP.NET with Webcontrols and in my test load the Side everytime new if I click on the Button. 我使用Webcontrols测试jQuery和ASP.NET,并且在我的测试中,如果我点击Button,每次都会加载Side。 The jQuery Code add a Css Class to a Label. jQuery代码将Css类添加到Label。

My aspx: 我的aspx:

      <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="jQueryAnwendung.Main" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>jQuery Anwendung</title>

    <script src="Scripte/jquery-1.8.0.min.js" type="text/javascript"></script>

    <style type="text/css">
        .CssKlasse 
        {
            background:red;
            font-size:40px; 
            }
    </style>

    <script type="text/javascript" language="javascript">

        $(document).ready(function () {
            $("#<%= b1.ClientID%>").click(function (event) {
                $("#<%= bild1.ClientID %>").slideToggle('slow');
            });

        });

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="b1" runat="server" text="Bild verkleinern/vergrößern"/>
        <asp:Image ID="bild1" runat="server" ImageUrl="~/Theme/net.png" Width="50" Height="50" /> 
    </div>
    </form>
</body>
</html>

What can I do? 我能做什么?

tarasov 塔拉索夫

The generated id is more verbose; 生成的id更详细; open your browser and click View source . 打开浏览器并单击“ 查看源” You will notice it. 你会注意到它。 Copy and paste the ID into your jQuery selector. 将ID复制并粘贴到jQuery选择器中。

Another way is to call b1.clientID inside your .aspx : $("<%= b1.ClientID %>") , which will output the generated clientID at runtime. 另一种方法是在.aspx$("<%= b1.ClientID %>")调用b1.clientID ,它将在运行时输出生成的clientID

Since ASP.NET 4, StaticMode allows you to have complete control over the generated ID. 从ASP.NET 4开始, StaticMode允许您完全控制生成的ID。 For more information, check it out: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx 有关更多信息,请查看: http//msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

if you using asp controls you have 2 ways to handle it 如果您使用asp控件,您有两种方法来处理它

First is change "#b1 and "#l1" to "<%= #b1.ClientID%>" and "<%= #l1.ClientID %>" in your jQuery function 首先是在你的jQuery函数"<%= #l1.ClientID %>" "#b1"#l1"更改为"<%= #b1.ClientID%>""<%= #l1.ClientID %>"

Second is to add attribute ClientIDMode="Static" to your asp:Button and asp:Label 其次是将属性ClientIDMode="Static"添加到asp:Buttonasp:Label

单击它时Asp按钮会导致Post返回 - 页面重新加载并且您的脚本未执行,您可以使用HTML按钮或尝试禁用此按钮上的回发。

inside your handler, prevent the button from behaving normally: 在处理程序内部,阻止按钮正常运行:

$("#<%= b1.ClientID%>").click(function (event) {
  event.preventDefault(); //prevent the control to act in its default way
  $("#<%= bild1.ClientID %>").slideToggle('slow');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM