Javascript pass arguments to WebMethod in C#

So, as we all know WebMethod generally cannot access any other parts of the code since it is normally a static and a web method. Therefore I found a quick and silly idea to get around with this. I have set labels hiden in the page and I’m passing their content as an argument when calling the WebMethod. This is done as shown below through Ajax Calls:


<script type=”text/javascript”>

$(document).ready(function() {

type: “POST”,
url: “default.aspx/GetMyWebMethod”,
data: “{arg1:'” + $(‘#<%= lbl_myFirstArgument.ClientID %>’).html() + “‘,arg2:'” + $(‘#<%= lbl_mySecondArgument.ClientID %>’).html() + “‘}”,

contentType: “application/json; charset=utf-8”,
dataType: “json”,
success: function(msg) {
//Do something on success





<div style=”display: none”>

<asp:Label ID=”lbl_myFirstArgument” runat=”server” Text=””></asp:Label>
<asp:Label ID=”lbl_mySecondArgument” runat=”server” Text=””></asp:Label>




//We will fill the content of the labels in the Page_Init in order to be ready on document.ready(function() {} );

protected void Page_Init(object sender, EventArgs e)

lbl_myFirstArgument.Text = DateTime.Now.toString();

lbl_mySecondArgument.Text = “Write Something here or get something dynamically from Database to pass to WebMethod”;


public static void GetMyWebMethod(string arg1, string arg2)

//You Can do something with arg1 and arg2 



That’s All !!!! Please feel free to comment or give suggestions on quick and easy alternatives 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s