Calling server side methods using JavaScript and JQuery in ASP.Net

Posted On // Leave a Comment

This is asked many times on asp.net forums,
1. How to call Server Side methods or functions using JavaScript in ASP.Net
2. How to call Server Side methods Client Side in ASP.Net
Now the answer is using jQuery.
jQuery allows you to call Server Side ASP.net methods from client side without any PostBack. Actually it is an AJAX call to the server but it allows us to call the method or function defined server side.
 
 
Syntax
The following picture describes the syntax of the jQuery AJAX call.
Calling Server Side web methods in ASP.Net using JQuery JavaScript
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net TextBox and an HTML Button.
<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick = "ShowCurrentTime()" />
</div>
 
 
Client Side Script
When the Button is clicked the ShowCurrentTime JavaScript function is executed which makes an AJAX call to the GetCurrentTime WebMethod. The value of the TextBox is passed as parameter to the WebMethod.
<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: "CS.aspx/GetCurrentTime",
        data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>
 
 
The WebMethod
The following WebMethod returns a greeting message to the user along with the current server time. An important thing to note is that the method is declared as static (C#) and Shared (VB.Net) and is decorated with WebMethod attribute, this is necessary otherwise the method will not be called from client side jQuery AJAX call.
C#
[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
        + DateTime.Now.ToString();
}
 
VB.Net
_
Public Shared Function GetCurrentTime(ByVal name As StringAs String
   Return "Hello " & name & Environment.NewLine & "The Current Time is: " & _
            DateTime.Now.ToString()
End Function
 
 
Screenshot
Calling Server Side Methods JavaScript and jQuery in ASP.Net


[Read more]

Get URL Address ASP.NET

Posted On // Leave a Comment
String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");

Result

[Read more]

Trigger UpdatePanel From ImageButton in GridView

Posted On // Leave a Comment
protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibTrue = (ImageButton)e.Row.FindControl("imgButtonTrue");                
                if (ibTrue != null) ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(ibTrue);

                ImageButton ibFalse = (ImageButton)e.Row.FindControl("imgButtonFalse");                
                if (ibFalse != null) ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(ibFalse);
            }
        }
asdf
[Read more]

Pemberitahuan Di Iphone Saat Commuterline Mengalami Gangguan

Posted On // Leave a Comment
Akhir-akhir ini kereta Commuterline sering kali mengalami gangguan. Dan walaupun account twitter @commuterline pasti memberitahukan gangguan tersebut, saya seringkali lupa untuk mengecek twitter.

nah, saya akan memberikan tips agar iPhone atau iPad kamu memberi notifikasi bahwa commuterline sedang mengalami gangguan. Jadi kamu bisa mengambil keputusan apakah akan menggunakan commuterline atau menggunakan transportasi lain.

1

1. Download aplikasi terbaru ifttt di app store lalu register jika kamu belum mempunyai account atau langsung login jika kamu sudah memiliki account ifttt.

2. Tap logo recipes (gambar mangkok) di pojok kanan atas kemudian tap logo tambah

2


3. Pada halaman create a recipe, tap icon plus berwarna biru

3


4. Di halaman Select Trigger, pilih Feed dan tap icon plus New Feed item
4

5. Masukan link http://feeds.feedburner.com/opqCommuter pada isian Feed Url lalu tap next
5


6.  Kembali ke halaman Create a Recipe, Tap icon plus berwarna orange

6

7. pada halaman Select Action, Pilih iOS Notifications dan tap pada icon Send a notification
7

8. Jika langkah2 diatas kamu ikuti dengan benar, seharusnya muncul halaman seperti berikut. tap finish 
8

Jika kamu mengikuti instruksi diatas dengan benar, iPhone mu akan memberi tahu saat commuterline mengalami gangguan, selamat mencoba


Semoga membatu

[Read more]

Regex Password

Posted On // Leave a Comment
(?=.*?[A-Z])(?=.*?[A-Z])(?=.*?[^a-zA-Z]).{8,}
string must contain lower case, upper case, number and min 8 character
[Read more]

Beli Rumah Sekarang!! 2014 Harga Rumah Dipastikan Naik

Posted On // Leave a Comment


Tahun depan, harga rumah naik 30%!
Email not displaying correctly?
View it in your browser.


Beli Rumah Sekarang! 2014 Harga Rumah Dipastikan Naik

Apakah Anda tahu bahwa tahun 2014 harga rumah naik sampai dengan 30%?
Buruan beli rumah sekarang!! sebelum harga naik!!

Kunjungi http://www.hargarumah.info untuk harga rumah, info properti terupdate
Selengkapnya »

Follow on Twitter | Friend on Facebook | Forward to Friend 
Copyright © 2013 IdeaBox Indonesia, All rights reserved.
friends

Our mailing address is:
IdeaBox Indonesia
Sentral Senayan I
Jakarta, Jk 11430

Add us to your address book

Email Marketing Powered by MailChimp
Mohon maaf jika email ini mengganggu. klik disini jika tidak ingin menerima email dari kami| update subscription preferences 
[Read more]