C
C#2mo ago
RV

✅ ASP.NET Web Form Web Method not return expected output.

ASP.NET Web Form doesn't return the value I want, but it returns the html of the page.
No description
10 Replies
RV
RVOP2mo ago
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PageMethodTest._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<main>
<button type="button" onclick="GetDetails('jj')">test </button>
</main>

<script type="text/javascript">
function GetDetails(Id) {
PageMethods.GetDetails(Id, (res) => {
alert(res)
});
}
</script>

</asp:Content>
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PageMethodTest._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<main>
<button type="button" onclick="GetDetails('jj')">test </button>
</main>

<script type="text/javascript">
function GetDetails(Id) {
PageMethods.GetDetails(Id, (res) => {
alert(res)
});
}
</script>

</asp:Content>
Default.aspx.cs
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[System.Web.Services.WebMethod]
public static string GetDetails(string Id)
{
return "hello";
}
}
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[System.Web.Services.WebMethod]
public static string GetDetails(string Id)
{
return "hello";
}
}
Site.master
<form runat="server">
<asp:ScriptManager ID="mainscr" runat="server" EnablePageMethods="true">
<Scripts>
<%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
<%--Framework Scripts--%>
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
<!-- other !-->
<form runat="server">
<asp:ScriptManager ID="mainscr" runat="server" EnablePageMethods="true">
<Scripts>
<%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
<%--Framework Scripts--%>
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
<!-- other !-->
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
RV
RVOP2mo ago
Yeah,
Angius
Angius2mo ago
The easiest and best way to solve any and all issues with Webforms, is to throw it in the trash where it belongs and use something normal
RV
RVOP2mo ago
Because net core cant use crystal report
Angius
Angius2mo ago
But .NET Framework — old and obsolete it might be — does not imply Webforms
RV
RVOP2mo ago
Yeah, but the current project is using crystal report and web form. Changes framework, change everything.
Angius
Angius2mo ago
Ah, well, my sincere condolences in that case
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
gerard
gerard2mo ago
The Javascript callback has three parameters: result, returnResult, method You're currently using the result I think you want to add another parameter, which returns the object you want. So instead of (res) => you want (_, res) =>

Did you find this page helpful?