If you want to pass a value from client-side to code behind without posting back the entire page, you will need to use Ajax.
Calling to a server-side method in ASP.Net Web Form is not as clean as ASP.Net Web API or MVC. You will need to use old WebMethod.
For example,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<button type="button" onclick="getData();">Get Data</button>
<br/>
<input type="text" name="StartDate" id="txtStartDate" maxlength="10" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function getData() {
var data = {value: "test"};
$.ajax({
type: "POST",
url: '<%= ResolveUrl("~/Default.aspx/GetCurrentDate") %>',
data: JSON.stringify(data),
contentType: "application/json",
success: function (msg) {
$("#txtStartDate").val(msg.d);
}
});
}
</script>
</form>
</body>
</html>
Code Behind
using System;
using System.Web.Script.Serialization;
namespace DemoWebForm
{
public partial class Default : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static string GetCurrentDate(string value)
{
return new JavaScriptSerializer().Serialize(
string.Format("{0} - {1}", DateTime.Now, value));
}
}
}