Sunday, September 26, 2010

How to save only changes (stop posting back if there are no any changes)

Demo:
For this we can utlize a nice little jQuery plugin called DirtyForm
Download dirty form:
Download jQuery:
Example:
<%@ Page Language="C#"  %>
<html>
<head id="Head1" runat="server">
 <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
 <script src="Scripts/jquery-dirtyform.js" type="text/javascript"></script>
 <script language="javascript" type="text/javascript">
     var isDirty = false;
     $(document).ready(function () {
         $('#<%= this.Form.ClientID %>')
     .dirty_form()
     .dirty(function (event, data) {
         isDirty = true;
     });
     });
     function Save() {
         if (!isDirty) {
             alert("You didn't change anything");
             return false;
         }
         return true;
     }
 </script>
 <script runat="server">
  protected void Save(object sender, EventArgs e)
  {
       this.lblUpdateTime.Text = string.Format("Last Updated On: {0}", DateTime.Now.ToString("hh:mm:ss"));
  }
 </script>
</head>
  <body>    
    <form id="form1" runat="server">
        <asp:TextBox runat="server" ID="txtName"></asp:TextBox><br />
        <asp:TextBox runat="server" ID="txtAddress"></asp:TextBox><br />
        <asp:TextBox runat="server" ID="txtPostcode"></asp:TextBox><br />
        <asp:TextBox runat="server" ID="txtEmail"></asp:TextBox><br />
        <asp:Button 
            runat="server" 
            ID="btnSave" 
            OnClick="Save" 
            OnClientClick="return Save();" 
            Text="Save" /><br />
        <asp:Label runat="server" ID="lblUpdateTime" />        
     </form>
   </body>
</html>

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...