Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

Monday, July 30, 2012

How can I save record using jQuery in ASP.Net?

Problem: How can I save record using jQuery in ASP.Net? Please provide me source code for save record using jquery in asp.net.

Solution:

You need to create a web service and add a web method for saving record and call the method from jquery.
Look at the following example:
You need to add a method in your web service which is marked with WebMethod attribute. Similar to following code snipppet:


[WebMethod]
        public bool MarkProjectComplete(long projectID)
        {
            bool result = false;
            EnumStatus status = ProjectFacade.MarkProjectComplete(projectID);
            if (status == EnumStatus.Successfull)
            {
                result = true;
                if (projectID > 0)
                {
                    Project project = null;
                    project = ProjectFacade.GetProjectByID(projectID);
                }
            }
            return result;
        }
 

Then call your method from your jquery function. Look at following code snippet:

function MarkProjectComplete(projectID) {
            if (confirm('Are you sure you would like to mark this project as ‘Mark Project Complete’?')) {
                $.ajax({
                    type: "POST",
                    url: "<%= ApplicationPath %>/WebServices/YourWebService.asmx/MarkProjectComplete",
                    data: "{'projectID' : '" + projectID + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d == true) {
                            alert("Mark Project Complete Successfully");
                            
                        }
                    },
                    error: function () {
                    }
                });
            }
        }

Hope it will be helpful. Thanks.


Sunday, July 29, 2012

How can I protect by asp.net button from click multiple time or double click at a time?

Problem:  How can I protect by asp.net button from click multiple time or double click at a time?

Solution:
When clicking in save or insert button a user can click multiple times very quickly that sometimes is similar to double click. If you do not protect users from double click while inserting data, it may insert multiple records at a time which is unexpected.  Asp.net developers frequently face the problem.

To protect from this problem you need to add small java script function.

If you are not using validation group in your page then the function will be:


        <script language="javascript" type="text/javascript">
        var crnt = 0;
        function PreventClicks() {

            if (typeof (Page_ClientValidate) == 'function') {
                Page_ClientValidate();
            }

            if (Page_IsValid) {
                if (++crnt > 1) {
                    alert(crnt);
                    return false;
                }
                return true;
            }
            else {
                return false;
            }
        }
    </script>



<asp:Button runat="server" CssClass="primaryAction" ID="btnInsertUser" OnClick="InsertNewUser" Text="Save" OnClientClick="return PreventClicks();" />


If you use validation group then the function will be:


    <script language="javascript" type="text/javascript">
        var crnt = 0;
        function  PreventClicks() {

            if (typeof (Page_ClientValidate('change-password')) == 'function') {
                Page_ClientValidate();
            }

            if (Page_IsValid) {
                if (++crnt > 1) {
                    alert(crnt);
                    return false;
                }
                return true;
            }
            else {
                return false;
            }
        }
    </script>


<asp:Button runat="server" CssClass="primaryAction" ID="btnInsertUser" 

ValidationGroup="change-password"

OnClick="InsertNewUser" Text="Save" dd OnClientClick="return PreventClicks();" />


Hope that it will be helpful for developers.

Thursday, August 25, 2011

How can I add external java script and css file dynamically in asp.net?

Problem: 
 I need to add a .css and .js file based on the user logged in. I have the css class attributeds added to all of the elements I need to style. What I need to do now is add the .css and .js file to the page on page load based on logged in user.

Solution:
The conventional way to loading external JavaScript (ie: .js) and CSS (ie: .css) files on a page is to stick a reference to them in the <head> section of your page, for example:
 
<head>
<script type="text/javascript" src="myscript.js"></script>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>

 But keeping JavaScript and CSS functions in a separate file (a .js and .css file) is highly recommended. Once they are in a separate file and part of a project, the file can be imported into a page using RegisterClientScriptBlock method.

For instance, a .js file can be included in an ASP.NET page using the following code:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript",
   "<script language=javascript src='MyJSFile.js'>");

 and for including a .css file same code should rewrite as follows:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyCSS", 
 "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/MyCSSFile.css\"/>");

 Once the .js file or .css is imported into the ASP.NET page, any of the JavaScript functions, css class and selectors can be called as before.