Link to home
Start Free TrialLog in
Avatar of pamela rizk
pamela rizkFlag for Lebanon

asked on

asp web application

i have an asp web Form form1 in asp.net  that contains a button click
on this button click i am opening  form2

i need from form1 to call a method used in form 2 how to do that ?
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

You can use AJAX to make the call to the method in your code behind of form 2.
$(document).ready(function() {
	$.ajax({
		type: "POST",
		url: "Form2.aspx/MyMethod",
		data: JSON.stringify({ id: id }),
		contentType: "application/json; charset=utf-8",
		dataType: "json"
	}).success(function (data) {
		obj = JSON.parse(data.d);
		console.log(obj);
	});
});

Open in new window


Your method must be decorated with the WebMethod attribute, and must be a static function

[WebMethod]
public static string MyMethod(string id)
{
     return JsonConvert.SerializeObject("hello world");
}

Open in new window


Another option is to pull your method into a class library and then reference / include it in both Form1 and Form2

The least favorable option would be to copy the method from Form2 into Form1
ASKER CERTIFIED SOLUTION
Avatar of Edward Wood
Edward Wood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial