Why my jquery code doesn't work with updatepanel post back?
Hello,
I'm new to jquery. I want to use jquery mask function to format my input.
Here is my original code:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="../Scripts/jquery-1.11.3.js" type="text/javascript"></script>
- <script src="../Scripts/jquery-maskmoney-3.0.2.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- $('#<%=TextBox1.ClientID%>').attr('maxlength', 8).maskMoney({prefix:'$',thousands:',',precision:0,'placeholder':''});
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="SM" runat="server"></asp:ScriptManager>
- <asp:UpdatePanel ID="upMain" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- </ContentTemplate>
- </asp:UpdatePanel>
- </form>
- </body>
- </html>
This code doesn't work with my solution. so I change jquery part of code to the following code, then it works with first page load.
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- $('#<%=TextBox1.ClientID%>').attr('maxlength', 8).maskMoney({prefix:'$',thousands:',',precision:0,'placeholder':''});
- });
- </script>
I change my jquery code to the following:
- <script type="text/javascript">
- function InIEvent() {
- $('#<%=TextBox1.ClientID%>').attr('maxlength', 8).maskMoney({prefix:'$',thousands:',',precision:0,'placeholder':''});
- }
- $(document).ready(InIEvent);
- </script>
and change html page to:
- <form id="form1" runat="server">
- <asp:ScriptManager ID="SM" runat="server"></asp:ScriptManager>
- <script type="text/javascript">
- Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InIEvent);
- </script>
- <asp:UpdatePanel ID="upMain" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- </ContentTemplate>
- </asp:UpdatePanel>
- </form>
Now all my code even doesn't work with first page load. So my question is what's the difference between
- jQuery(document).ready(function($) {
- $('#<%=TextBox1.ClientID%>').attr('maxlength', 8).maskMoney({prefix:'$',thousands:',',precision:0,'placeholder':''});
- });
and
- function InIEvent() {
- $('#<%=TextBox1.ClientID%>').attr('maxlength', 8).maskMoney({prefix:'$',thousands:',',precision:0,'placeholder':''});
- }
- $(document).ready(InIEvent);
Is there anything wrong with my code? Same code works with a brand new application.
Thanks for your helping.