Friday 23 October 2015

how to call ajax with Jquery using Asp.net

how to call ajax with Jquery using Asp.net
Hello friends here i am show you a example how to ajax call using asp.net server side code:


Many of the times the interviewer asked the question that how to call how to call server side method as a client side in Asp.net.

so the answer is using jquery we can use server side method via client side:
here is example for this.


first of all create a sample web application in visual studio

1. visual studio 2012->new project - web application .


now on default.aspx page write the following code in body tag.


<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()" />


after that call the jquery within head section like this ..


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function ShowCurrentTime()
        {
            $.ajax({
                type: "POST",
                url: "WebForm1.aspx/GetCurrentTime",
                data: '{name:"' + $("#<%=txtUserName.ClientID%>")[0].value + '"}',
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (responce) {
                    alert(responce.d);
                }
            });
        }
        function OnSuccess(responce) {
            alert(responce.d);
        }
    </script>

you can see easily the above code
in the first line we reference the jquery file user can also download this jquery and reference it to locally.

now in function ShowCurrentTime() we make ajax call here $.ajax is signature for call ajax method

here type may be post or get according to user requirement.

now it is main part on default.aspx.cs page make method to as webmethod which is necessary to call server method on client side

 [System.Web.Services.WebMethod]
        public static string GetCurrentTime(string name)
        {
            return " Hello " + name + Environment.NewLine + "The Current Time is :" + DateTime.Now.ToString();
        }


You can test this demo and enjoy..

Tuesday 6 October 2015

How to count the occurence of a string counted without using for loop in a sentence.

Here we use a simple way to count the string occurence in a sample string without using for loop.



first of all we use a name space using System.Text.RegularExpressions

then take a sample string here i take this


string s="One reason people lie is to achieve personal power. Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date. However, I didn’t have a girlfriend. One of my other friends, who had a date to go to the party with, asked me about my date. I didn’t want to be embarrassed, so I claimed that I had a lot of work to do. I said I could easily find a date even better than his if I wanted to. I also told him that his date was ugly. I achieved power to help me feel confident; however, I embarrassed my friend and his date. Although this lie helped me at the time, since then it has made me look down on myself."

Now the code for counting the purticular string


int count=Regex.Matches(s.ToUpper(), "THE").Count;

and now print on console like this

Console.WriteLine(count);

Thursday 17 September 2015

How to Remove Comma Character from the Last of string.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>remove last comma from string jquery</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('.btnRemove').click(function (event) {
                var strVal = $.trim($('.txtValue').val());
                var lastChar = strVal.slice(-1);
                if (lastChar == ',') {
                    strVal = strVal.slice(0, -1);
                }
                $("#result").text(strVal);
            });
        });
    </script> 
</head>
<body>
    <div>
        <table>
            <tr>
                <td>
                    <input type="text" class="txtValue" value="I am Neeraj upadhyay, " />
                </td>
                <td>
                    <button class="btnRemove">Remove</button>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="result" style="color:red;"></div>
                </td>
            </tr>  
    </div>
</body>
</html>

AdSense

The Ultimate Guide to Interceptors: Understanding Their Power and Functionality

  The Ultimate Guide to Interceptors: Understanding Their Power and Functionality An interceptor is a service that can intercept HTTP reques...

Follow