저번에 다른 글에서 숨겨진 컨트롤을 이용한 비하인드 코드를 호출하는 방법을 알려 드렸습니다.
(참고 : [ASP.Net] 자바스크립트에서 비하인드 호출하기)
다른방법으로 스크립트 매니저를 이용한 방법이 있습니다.
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/> <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input id="Button1" type="button" value="button" onclick='CallMethod();' /> </form>
[System.Web.Services.WebMethod]
public static string GetDate(string sMsg)
{
return DateTime.Now.ToString() + " " + sMsg;
}
<script type="text/javascript" language="javascript">
function CallMethod()
{
//값 가저오기
var sMsg = document.getElementById('TextBox1').value;
//비하인드 코드 호출
PageMethods.GetDate(sMsg, CallSuccess);
}
function CallSuccess(sReturn)
{
//성공했다!
alert(sReturn);
}
</script>