c# - How to make client side click event handler to request a server side method -
in details: load html code onto page , upon click of <a>
link, want click event captured , handled code-behind.
here i've got client-side code:
$('a').click(function (e) { event.preventdefault(); var data = { username: $(this).attr("id") }; var dataval = json.stringify(data); $.ajax({ type: "post", url: "default.aspx/loadnewpage", contenttype: "application/json; charset=utf-8", data: dataval, datatype: "json", success: function (id) { } }); });
html:
<a href="#" id="kontakt">go kontakt</a>
the problem code-behind function not called, #
added url...
i'd appreciate corrections on code code examples , maybe explanation on them.
update: tried asp page webmethods. code-behind:
[webmethod] public string loadnewpage(string id) { ltlcontentcenter.text = getpagecenter("kontakt"); ltlcontentside.text = getpageside("kontakt"); return "hello"; }
js code:
function loadme() { pagemethods.loadnewpage("kontakt", callsuccess, callerror); } function callsuccess(res) { alert(res); } function callerror() { alert('error'); }
the html:
<body> <form id="form1" runat="server"> <asp:scriptmanager id="scpt" runat="server" enablepagemethods="true" /> <div id="parent"> <div id="mainmenu"> <asp:linkbutton id="linkbutton1" text="forsiden" commandargument="forsiden" oncommand="loadpage_command" runat="server" /> <br /> <asp:linkbutton id="linkbutton2" text="produkter" commandargument="produkter" oncommand="loadpage_command" runat="server" /> <br /> <asp:linkbutton id="linkpages" text="support" commandargument="media" oncommand="loadpage_command" runat="server" /> <br /> <asp:linkbutton id="linkbutton3" text="kontakt" commandargument="kontakt" oncommand="loadpage_command" runat="server" /> <br /> <asp:linkbutton id="linkbutton4" text="om abix" commandargument="omabix" oncommand="loadpage_command" runat="server" /> <br /> </div> <div id="logo"> </div> <asp:literal id="ltlcontentcenter" runat="server" /> <asp:literal id="ltlcontentside" runat="server" /> <a href="#" onclick="loadme()">click me, please.</a> </div> </form> </body>
the browser console gives me error "pagemethods not defined"
.
ok..not jquery way more of c# way access server side using client script..try using page methods
first add script manager on aspx page
<body> <form id="form1" runat="server"> <asp:scriptmanager id="scpt" runat="server" enablepagemethods="true"> </asp:scriptmanager> </form> </body>
then go aspx.cs page , declare function like
[system.web.services.webmethod] public static string validateuser(string emailid, string password) { //your logic code return "hello server side"; }
then javascript call c# method like
pagemethods.validateuser(email, password, callsuccess_login, callfailed_login);
and in ur javascript create 2 call functions callsuccess_login , callfailed_login
hope helps
Comments
Post a Comment