js失效是因为Updatapanel没有postback, js不会重新加载,所以失效。我们必须在页面中的Updatepanel中重新加载js,童鞋们请参考以下例子:
<%@ Page Title="About Us" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="About.aspx.vb" Inherits="JavaScriptSample.About" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript"> $(function() { BindEvents(); });
function BindEvents() {
$(":checkbox:first").bind('click', function () { if (this.checked == true) { $(":checkbox:gt(0)").attr('checked', 'true'); } else { $(":checkbox:gt(0)").removeAttr("checked") } }); $(":checkbox:gt(0)").bind('click', function () { var checkedCounter = 0; $(":checkbox:gt(0)").each(function () { if (this.checked == true) { checkedCounter++; } }); if (checkedCounter == 4) { $(":checkbox:first").attr('checked', 'true'); } else { $(":checkbox:first").removeAttr("checked") } });
} </script> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <script type="text/jscript" > var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_endRequest(function () { // re-bind your jquery events here BindEvents(); }); </script> <asp:CheckBox ID="cbAll" runat="server" Text="cbAll" /> <asp:CheckBox ID="CheckBox1" runat="server" Text="cb1" /> <asp:CheckBox ID="CheckBox2" runat="server" Text="cb2" /> <asp:CheckBox ID="CheckBox3" runat="server" Text="cb3" /> <asp:CheckBox ID="CheckBox4" runat="server" Text="cb4" /> <asp:Button runat="server" ID="btn1" Text="Text" /> </ContentTemplate> </asp:UpdatePanel> </asp:Content>
呵呵!希望对您有用!